My question is somewhat similar to this SO but not the same.
我的问题与此类似,但不一样。
I created a HelloWorld
program with the following:
我创建了一个HelloWorld程序,其中包含以下内容:
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:
当我生成项目文件(例如Visual Studio .sln文件或XCode .xcodeproj文件)时。我想点击运行按钮并在执行程序时将一些命令行参数传递给HelloWorld,如下所示:
./HelloWorld --gtest_filter=Test_Cases1*
Also see this SO for how this is done in Visual Studio.
另请参阅此SO以了解如何在Visual Studio中完成此操作。
Is it possible to do this in CMakeList file? If not, why?
是否可以在CMakeList文件中执行此操作?如果没有,为什么?
5 个解决方案
#1
CMake 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.
CMake没有内置支持。原因是Visual Studio Project属性的Debugging选项卡中的设置不存储在项目文件(.vc [x] proj)中,而是存储在特定于用户和计算机的.user文件中,并且CMake不会生成这些。
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.
您可以在CMake中自己编写代码(我在工作中为我们的框架执行了此操作)。该文件只是XML,因此您可以根据需要预先填充它。它的结构很容易理解。例如,正在调试的程序的命令行参数存储在
#2
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.:
为什么这么复杂?如此处所述,VS和CMake支持开箱即用。在VS中打开CMakeLists.txt文件,然后打开“调试和启动设置”(例如,通过“CMake”菜单或右键单击“解决方案资源管理器”。
Add your program arguments to "args" in the file "launch.vs.json" which pops up.
将程序参数添加到弹出的“launch.vs.json”文件中的“args”中。
{
"version": "0.2.1",
"defaults": {},
"configurations": [
{
"type": "default",
"project": "CMakeLists.txt",
"projectTarget": "tests\\hellotest",
"name": "tests\\hellotest with args",
"args": ["argument after argument"]
}
]
}
#3
CMake 3.13.0 looks like it will add support for this in the form of the following target properties:
CMake 3.13.0看起来会以下列目标属性的形式添加对此的支持:
-
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.
VS_DEBUGGER_COMMAND_ARGUMENTS - 设置Visual Studio C ++目标的本地调试器命令行参数。
VS_DEBUGGER_ENVIRONMENT - 为Visual Studio C ++目标设置本地调试器环境。
It extends use with these commands, available since CMake 3.12:
它扩展了使用这些命令,从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.
VS_DEBUGGER_COMMAND - 为Visual Studio C ++目标设置本地调试器命令。
VS_DEBUGGER_WORKING_DIRECTORY - 设置Visual Studio C ++目标的本地调试器工作目录。
#4
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:
如果您要将E:\ dev \ IfcPL \ examples \ Trassierung.ifc作为参数添加到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)
#5
Not a CMake trick. You can do this to set default args
for debug builds:
不是一个CMake技巧。您可以这样设置调试版本的默认参数:
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... ]
#1
CMake 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.
CMake没有内置支持。原因是Visual Studio Project属性的Debugging选项卡中的设置不存储在项目文件(.vc [x] proj)中,而是存储在特定于用户和计算机的.user文件中,并且CMake不会生成这些。
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.
您可以在CMake中自己编写代码(我在工作中为我们的框架执行了此操作)。该文件只是XML,因此您可以根据需要预先填充它。它的结构很容易理解。例如,正在调试的程序的命令行参数存储在
#2
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.:
为什么这么复杂?如此处所述,VS和CMake支持开箱即用。在VS中打开CMakeLists.txt文件,然后打开“调试和启动设置”(例如,通过“CMake”菜单或右键单击“解决方案资源管理器”。
Add your program arguments to "args" in the file "launch.vs.json" which pops up.
将程序参数添加到弹出的“launch.vs.json”文件中的“args”中。
{
"version": "0.2.1",
"defaults": {},
"configurations": [
{
"type": "default",
"project": "CMakeLists.txt",
"projectTarget": "tests\\hellotest",
"name": "tests\\hellotest with args",
"args": ["argument after argument"]
}
]
}
#3
CMake 3.13.0 looks like it will add support for this in the form of the following target properties:
CMake 3.13.0看起来会以下列目标属性的形式添加对此的支持:
-
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.
VS_DEBUGGER_COMMAND_ARGUMENTS - 设置Visual Studio C ++目标的本地调试器命令行参数。
VS_DEBUGGER_ENVIRONMENT - 为Visual Studio C ++目标设置本地调试器环境。
It extends use with these commands, available since CMake 3.12:
它扩展了使用这些命令,从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.
VS_DEBUGGER_COMMAND - 为Visual Studio C ++目标设置本地调试器命令。
VS_DEBUGGER_WORKING_DIRECTORY - 设置Visual Studio C ++目标的本地调试器工作目录。
#4
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:
如果您要将E:\ dev \ IfcPL \ examples \ Trassierung.ifc作为参数添加到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)
#5
Not a CMake trick. You can do this to set default args
for debug builds:
不是一个CMake技巧。您可以这样设置调试版本的默认参数:
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... ]