Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cmake 向项目中添加命令行参数_Cmake - Fatal编程技术网

Cmake 向项目中添加命令行参数

Cmake 向项目中添加命令行参数,cmake,Cmake,我的问题与此有些相似,但不尽相同 我用以下内容创建了一个HelloWorld程序: add_executable( HelloWorld ${SRC} ) 生成项目文件时(例如Visual Studio.sln文件或XCode.xcodeproj文件)。我想在执行程序时点击run按钮并将一些命令行参数传递给HelloWorld,如下所示: ./HelloWorld --gtest_filter=Test_Cases1* 另请参见本文,了解如何在VisualStudio中执行此操作 是否可以在

我的问题与此有些相似,但不尽相同

我用以下内容创建了一个
HelloWorld
程序:

add_executable( HelloWorld ${SRC} )
生成项目文件时(例如Visual Studio.sln文件或XCode.xcodeproj文件)。我想在执行程序时点击run按钮并将一些命令行参数传递给
HelloWorld
,如下所示:

./HelloWorld --gtest_filter=Test_Cases1*
另请参见本文,了解如何在VisualStudio中执行此操作


是否可以在CMakeList文件中执行此操作?若否,原因为何

这个答案是在CMake 3.13发布之前写的,所以现在已经过时了。还有一个可用的


CMake 3.12及以下版本对此没有内置支持。原因是Visual Studio项目属性的“调试”选项卡中的设置不是存储在项目文件(
.vc[x]proj
)中,而是存储在特定于用户和机器的
.user
文件中,CMake不会生成这些设置


您可以在CMake中自己编写(我在工作中为我们的框架编写了代码)。该文件只是XML,因此您可以根据需要预先填充它。它的结构很容易理解。例如,正在调试的程序的命令行参数存储在
XML元素(嵌套在
中)的
CommandArguments
属性中。

不是CMake技巧。您可以这样做,为调试生成设置默认的
args

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... ]
intmain(intargc,char*argv[])
{const char*command=argv[1];
如果(argc<2)
{             
#ifdef_调试
command=“你好”;
#否则
用法();
返回1;
#恩迪夫
}
[进程命令参数…]

如果要添加
E:\dev\IfcPL\examples\Trassierung.ifc
作为Visual Studio 2017中调试生成项目的参数:

project.vcxproj.user.in:

看起来它将以以下目标属性的形式对此进行设置:

  • 代码> vsdUngGrace~命令>参数>代码> -设置VisualStudioC++目标的本地调试器命令行参数。
  • > VSU-Debug GuestEngult -设置VisualStudioC++目标的本地调试器环境。
它扩展了自CMake 3.12以来可用的这些命令的使用:

    < LI>代码> VSU-Debug Grords< /Cord>——为VisualStudioC++目标设置本地调试器命令。 < C++ >代码> vs*Debug GoWorksDirectory < /Cord>——为VisualStudioC++目标设置本地调试器工作目录。
为什么如此复杂?VS和CMake支持上述开箱即用功能。 在VS中打开CMakeLists.txt文件,并打开“调试和启动设置”(例如,通过“CMake”菜单或通过右键单击“解决方案资源管理器”。例如:

将程序参数添加到弹出的文件“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"]
    }
  ]
}

正如其他人所指出的,这很可能是特定于IDE的。它可能在VS中工作,但在其他IDE中不工作?例如,您是否有机会在Xcode中测试这一点?VS express不支持这一点这似乎是使用
Visual Studio
CMake
时的最佳解决方案,感谢您的分享。这个答案已经不长了r只要您使用的是CMake>=3.13.0就有效。请参见我的建议,这不是一个坏主意,完全独立于任何构建环境(当然,只要它设置了_DEBUG)。但是当每个开发人员的参数必须不同时,它就不起作用。
add_executable(HelloWorld main.cpp) 
configure_file(project.vcxproj.user.in ${CMAKE_BINARY_DIR}/HelloWorld.vcxproj.user) 
{
  "version": "0.2.1",
  "defaults": {},
  "configurations": [
    {
      "type": "default",
      "project": "CMakeLists.txt",
      "projectTarget": "tests\\hellotest",
      "name": "tests\\hellotest with args",
      "args": ["argument after argument"]
    }
  ]
}