C语言 cmake 命令行选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19564791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:48:53  来源:igfitidea点击:

cmake command line option

cvisual-studiocmake

提问by Aditya Sehgal

I am new to VS development and Cmake. I have used CMake-GUI to generate a visual studio solution and am able to build it successfully.

我是 VS 开发和 Cmake 的新手。我已经使用 CMake-GUI 生成了一个 Visual Studio 解决方案,并且能够成功构建它。

However, our code has now reached a stage where we can finally build & link into a binary. With multiple people checking in code, we want to do a nightly build and so I was thinking of writing a batch file for this.

但是,我们的代码现在已经到了可以最终构建和链接到二进制文件的阶段。由于多人签入代码,我们希望每晚进行一次构建,因此我正在考虑为此编写一个批处理文件。

However, I am trying to invoke cmake from command line and am running into issues.

但是,我试图从命令行调用 cmake 并且遇到了问题。

In cmake-gui, in order to configure, I provide two values Path of my source code Path where binaries will be generated

在 cmake-gui 中,为了配置,我提供了两个值 Path of my source code 将生成二进制文件的路径

However, when I try to run the same over command line (using the following command)

但是,当我尝试在命令行上运行相同的命令时(使用以下命令)

cmake -G "NMake Makefiles" -D CMAKE_SOURCE_DIR="D:\source_code" -D CMAKE_BINARY_DIR="D:\source_code\build\gen\host"

CMake throws up an eror : The source directory "D:\" is a file not a directory.

CMake 抛出一个错误:源目录“D:\”是一个文件而不是一个目录。

I tried the following variations too without any luck

我也尝试了以下变体,但没有任何运气

cmake -G "NMake Makefiles" -D PROJECT_SOURCE_DIR="D:\source_code" -D PROJECT_BINARY_DIR="D:\source_code\build\gen

Can someone please guide me to the correct syntax.

有人可以指导我使用正确的语法。

Thanks in advance

提前致谢

采纳答案by Fraser

You shouldn't try and set any of these variables on the command line. They're automatically set by CMake the first time it reads the CMakeLists.txt.

您不应该尝试在命令行上设置任何这些变量。它们是由 CMake 在第一次读取 CMakeLists.txt 时自动设置的。

Instead, you should runthe CMake command from within the binary dir, and pass the path to the directory containing the top-level CMakeLists.txt. So something like:

相反,您应该从二进制目录中运行CMake 命令,并将路径传递到包含顶级 CMakeLists.txt 的目录。所以像:

cd D:\source_code\build\gen\host
cmake -G"NMake Makefiles" D:\source_code

By the way, CMake's command line parsing isn't great (e.g. see this answer). I'd recommend avoiding leaving spaces after -Darguments.

顺便说一句,CMake 的命令行解析不是很好(例如,请参阅此答案)。我建议避免在-D参数后留下空格。

回答by Mahi

Try this

尝试这个

Remove space between -D and parameter

删除 -D 和参数之间的空格

 cmake -G "NMake Makefiles" -DCMAKE_SOURCE_DIR="D:\source_code" -DCMAKE_BINARY_DIR="D:\source_code\build\gen\host"

回答by jojy joseph

In my project, We used a CMake-GUI. But I created a shell script file to avoid repeatedly fill up the entries.

在我的项目中,我们使用了 CMake-GUI。但是我创建了一个shell脚本文件以避免重复填写条目。

Sample gist is as follows.

示例要点如下。

cd <DIRECTORY_TO_BUILD> && <CMAKE-INSTALLATION-PATH>/bin/cmake.exe --build= "<BUILDSOURCE-PATH> -DQt5Widgets_DIR:PATH=C:/Qt/5.12.0/msvc2017/lib/cmake/Qt5Widgets

I have started with cd else the script will create the build folder from where the script is run.

我从 cd 开始,否则脚本将从运行脚本的位置创建构建文件夹。