I use CMake to configure and generate makefiles for a Microsoft MPI project that I build and run from Visual Studio 2017. In order to run the project, I need to modify the VS solution configuration settings. Under Configuration Settings->Debugging, I want to specify "Command" and "Command Arguments" from the CMakeLists.txt. I can do this manually but I want to set it up from the CMakeLists.txt. Are there commands to do this?
CMake 3.12 introduced two new target properties for that purpose: VS_DEBUGGER_COMMAND and VS_DEBUGGER_COMMAND_ARGUMENTS. Set these properties in the following way:
set_target_properties(targetName PROPERTIES
VS_DEBUGGER_COMMAND "debug_command"
VS_DEBUGGER_COMMAND_ARGUMENTS "debug_arguments")
for anyone look for a solution on visual studio code with cmake extension, here is a solution:
change program field in launch.json to ${command:cmake.launchTargetPath},
launch.json
ref:
link
Related
Is there a command in CMake to set the value in Visual Studio when you right click on a project, go to Properties, then Debugging->Environment.
I like to put a string like this in that value:
PATH=C:/libs/vtk/bin/bin/$(Configuration);%PATH%
This allows me to maintain where DLLs are for a project on a project-by-project basis. Any help would be appreciated, thanks!
See Support VS_DEBUGGER_ENVIRONMENT in cmake projects.
You need to add the following CMake code fragment in your project CMakeLists.txt
if(WIN32)
set_target_properties(chebyshev_polynomials_demo
PROPERTIES VS_DEBUGGER_ENVIRONMENT "PATH=%PATH%;$<TARGET_FILE_DIR:Qt5::Core>;"
)
endif()
I have a multiplatform CMake project, and occasionally I have to build it manually for Windows. I generate a buildsystem like this:
cmake -DCMAKE_BUILD_TYPE=Release -G"Visual Studio 16 2019" -A x64 ../path/to/source
Then I open *sln file and press F7 to build. It runs for 40 minutes, and after that I understand that I didn't select proper configuration in the combobox. It's annoying! When command line option was -DCMAKE_BUILD_TYPE=Release, but combobox was set to Debug, build fails after spending a decent time.
Is it possible to generate an MSVS project with build configuration selected from command line?
Note that I'm aware of msbuild command and it's -p:Configuration=xxxxx flag. The question is about cases when for some reason you need to build from Visual Studio's GUI.
Changing the selected configuration for the GUI is not possible with CMake at this moment.
The main reason for this is that this information is stored in the .suo file generated by Visual Studio. CMake itself only generates the project and solution files, while the .suo file will be generated by Visual Studio itself.
Alternatively, use CMake's command line build option for this. After configuring your project and generating the VS .sln file from CMake as usual, simply run:
cmake --build <path_to_build_directory> --config Release
This works independently of the selected generator and is the most reliable way of building CMake projects.
I have a pretty large software library using CMake to be compiled. We use Linux mostly, but now a new colleague wants to use Visual Studio.
Is there any way to create a new VS 2017 project from the existing source codes with CMake structure?
I know, it's possible to do it with CLion, but I have no idea about VS, as I have a very little experience with it.
Other questions seem to focus on creating an empty project, which will use CMake, but not on creating a project from already existing source files.
Creating a cmake project with visual studio
Creating a project with visual studio 2017
I'm not sure why you asked for details but...
Assuming you are using cmake 3.13 then you can do the following in a command shell:
cmake -G "Visual Studio 15 2017" -S path_to_source -B path_to_build
This will then create a solution file. Actually it creates a solution file for every project() command that is issued in CMakeLists.txt.
You can then open the solution file in Visual Studio, and build the project as usual.
You don't even need to do this in the Visual Studio GUI. After creating the initial project you can also issue the command:
cmake --build path_to_build
Which will kick off the build at the command line.
Now if your CMakeLists.txt in path_to_source is using Linux specific libraries or gcc specific compiler settings then the CMakeLists.txt will have to get updated to the Windows equivalent.
The alternative is to start Visual Studio and then use File->Open->CMake and open the CMakeLists.txt file in path_to_source. It'll then start to generate the project. But I prefer using the command line method.
I have a Visual Studio 2015 solution generated by CMake. CMake created a "INSTALL" project that copies all the files I requested (using Cmake's install command in my CMakeLists.txt files).
This "INSTALL" project is skipped when I request a full solution build
I tried to add set_target_properties(INSTALL PROPERTIES EXCLUDE_FROM_ALL FALSE) but this reports set_target_properties Can not find target to add properties to: INSTALL.
How can I make "INSTALL" be generated by default? I'd like the checkbox surrounded in red in screenshot below to be enabled automatically:
You can use CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD:
set(CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD 1)
After some checking, it seems that there is a single common function that sets which projects are part of default build.
cmGlobalVisualStudio7Generator::IsPartOfDefaultBuild
Here is the part that does the check:
const std::string propertyName =
"CMAKE_VS_INCLUDE_" + *t + "_TO_DEFAULT_BUILD";
// inspect CMAKE_VS_INCLUDE_<*t>_TO_DEFAULT_BUILD properties
So as was mentioned in Florian's answer, you should be able to use CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD as well any custom project using
set(CMAKE_VS_INCLUDE_<custom project name>_TO_DEFAULT_BUILD 1)
Moreover, the variable can be added as an option on cmake launch from command line.
For VS 2017:
cmake -G "Visual Studio 15 2017 Win64" -DCMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD=ON ..
In my vs2013 project file, I have "Program database file name" (from Project Properties->C/C++->Output Files) to $(IntDir)vc$(PlatformToolsetVersion).pdb.
I know how to do this manualy, but for cmake i don't know.
How can I set this property from cmake?
You can use target properties COMPILE_PDB_NAME for the name and COMPILE_PDB_OUTPUT_DIRECTORY for the directory.
If you want to use the Visual Studio variables, you can try to use it directly in the variables above. If that's not working, a fallback is always to add the corresponding compiler options. In you case /Fd:
if (MSVC)
add_compile_options("/Fd\"$(IntDir)vc$(PlatformToolsetVersion).pdb\"")
endif()
or with generator expressions:
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/Fd\"$(IntDir)vc$(PlatformToolsetVersion).pdb\">")