26

My question is somewhat similar to this SO but not the same.

I created a HelloWorld program with the following:

add_executable( HelloWorld ${SRC} ) 

When I generate a project file (for example a Visual Studio .sln file, or an XCode .xcodeproj file). I want to hit the run button and pass in some command line arguments to HelloWorld when it executes the program, like the following:

./HelloWorld --gtest_filter=Test_Cases1* 

Also see this SO for how this is done in Visual Studio.

Is it possible to do this in CMakeList file? If not, why?

6 Answers 6

42

Why so complicated? VS and CMake support this out of the box as described here. Open the CMakeLists.txt file in VS, and open the "Debug and launch settings" (e.g. via the "CMake" menu or via right-click in the "Solution Explorer". E.g.:

enter image description here

Add your program arguments to "args" in the file "launch.vs.json" which pops up.

{ "version": "0.2.1", "defaults": {}, "configurations": [ { "type": "default", "project": "CMakeLists.txt", "projectTarget": "tests\\hellotest", "name": "tests\\hellotest with args", "args": ["argument after argument"] } ] } 
Sign up to request clarification or add additional context in comments.

9 Comments

This is most likely IDE specific as others have pointed out. It may have work in VS but not in other IDEs? Do you have a chance to test this out in Xcode for example?
VS express does not support this
This seems to be the best solution when working with Visual Studio and CMake, thanks for sharing this.
@RaulLuna In VS2019 16.11, (Top menu) Debug -> Debug and Launch Settings for <current project name> . Screenshot: i.sstatic.net/grryQ.png
not worked in vs2022
|
20

CMake 3.13.0 looks like it will add support for this in the form of the following target properties:

  • VS_DEBUGGER_COMMAND_ARGUMENTS - Sets the local debugger command line arguments for Visual Studio C++ targets.
  • VS_DEBUGGER_ENVIRONMENT - Sets the local debugger environment for Visual Studio C++ targets.

It extends use with these commands, available since CMake 3.12:

  • VS_DEBUGGER_COMMAND - Sets the local debugger command for Visual Studio C++ targets.
  • VS_DEBUGGER_WORKING_DIRECTORY - Sets the local debugger working directory for Visual Studio C++ targets.

2 Comments

How do we use these?
with set_target_properties command - cmake.org/cmake/help/latest/command/…: set_target_properties(HelloWorld PROPERTIES VS_DEBUGGER_COMMAND_ARGUMENTS "--gtest_filter=Test_Cases1*"
9

This answer was written before CMake 3.13 was released, so it's now outdated. There is an answer showing solution with CMake 3.13+ also available.


CMake 3.12 and below has no built-in support for this. The reason is that the settings from the Debugging tab of Visual Studio Project properties are not stored in the project file (.vc[x]proj), but in a user-and-machine-specific .user file, and CMake does not generate these.

You can code it yourself in CMake (I did that for our framework at work). The file is just XML, so you can pre-populate it according to your needs. Its structure is pretty easy to understand. The command-line arguments for the program being debugged are stored in the CommandArguments attribute inside a <DebugSettings> XML element (nested in <Configurations><Configuration>), for example.

1 Comment

This answer is no longer valid as long as you are using CMake >= 3.13.0. See my answer below
7

A solution for Visual Studio 2022 is clicking on the Debug menu bar item and then selecting Debug and Launch Settings for <Project Name>:

In the following launch.vs.json that opens, you can add your arguments at the bottom:

Now, when you start debugging using that profile, your desired command line arguments are passed:

1 Comment

I don't get any JSON, pressing that menu option does absolutely nothing..
1

In the case you want to add E:\dev\IfcPL\examples\Trassierung.ifc as an argument to a debug build project in Visual Studio 2017:

project.vcxproj.user.in:

<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <LocalDebuggerCommandArguments>E:\dev\IfcPL\examples\Trassierung.ifc</LocalDebuggerCommandArguments> <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> </PropertyGroup> </Project> 

CMakeLists.txt:

add_executable(HelloWorld main.cpp) configure_file(project.vcxproj.user.in ${CMAKE_BINARY_DIR}/HelloWorld.vcxproj.user) 

Comments

-2

Not a CMake trick. You can do this to set default args for debug builds:

int main(int argc,char* argv[]) { const char* command = argv[1]; if(argc < 2) { #ifdef _DEBUG command="hello"; #else Usage(); return 1; #endif } [ process command arg... ] 

3 Comments

This is not a bad idea, and completely independent of any build environment (well, as long as it sets _DEBUG).
But it does not work when the argument must be different for each developer.
Before doing this, I would prefer implementing a config file of some kind. Avoids recompiling every time the command line arguments change.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.