- 1 year ago
How can compile a simple windows application using cmake and Visual Studio 2019?
To compile a simple Windows application using CMake and Visual Studio 2019, follow these steps:
Set up the project structure: Create a new directory for your project and navigate to it in the command prompt or terminal.
Create a CMakeLists.txt file: Create a text file named
CMakeLists.txt
in the project directory. Open the file and add the following content:cmakecmake_minimum_required(VERSION 3.21)
project(YourProjectName)
add_executable(YourExecutableName main.cpp)
Replace
YourProjectName
with the desired name for your project, andYourExecutableName
with the desired name for your executable. Also, make sure to specify the appropriate source file(s) for your application in theadd_executable()
command.Generate the Visual Studio solution: Run CMake to generate the Visual Studio solution by executing the following commands:
bashcmake -S . -B build -G "Visual Studio 16 2019"
This will generate the Visual Studio solution in the
build
directory using the specified generator for Visual Studio 2019.Open the solution in Visual Studio: Navigate to the
build
directory and open the generated Visual Studio solution file (YourProjectName.sln
) in Visual Studio 2019.Build the project: In Visual Studio, select the desired build configuration (e.g., Debug or Release) and click the "Build" menu, then choose "Build Solution" or press Ctrl+Shift+B.
This will compile your application and generate the executable in the build output directory (by default,
build/Debug
orbuild/Release
).Run the application: Once the build is successful, you can run the executable directly from Visual Studio or locate the generated executable in the build output directory and run it manually.
That's it! You have successfully compiled a simple Windows application using CMake and Visual Studio 2019. You can further customize your CMakeLists.txt file to include additional source files, dependencies, or specific build configurations as needed.