<i id='PpwE3'><tr id='PpwE3'><dt id='PpwE3'><q id='PpwE3'><span id='PpwE3'><b id='PpwE3'><form id='PpwE3'><ins id='PpwE3'></ins><ul id='PpwE3'></ul><sub id='PpwE3'></sub></form><legend id='PpwE3'></legend><bdo id='PpwE3'><pre id='PpwE3'><center id='PpwE3'></center></pre></bdo></b><th id='PpwE3'></th></span></q></dt></tr></i><div id='PpwE3'><tfoot id='PpwE3'></tfoot><dl id='PpwE3'><fieldset id='PpwE3'></fieldset></dl></div>
    • <bdo id='PpwE3'></bdo><ul id='PpwE3'></ul>
  • <tfoot id='PpwE3'></tfoot>

        <legend id='PpwE3'><style id='PpwE3'><dir id='PpwE3'><q id='PpwE3'></q></dir></style></legend>

        <small id='PpwE3'></small><noframes id='PpwE3'>

        如何使用 CMake 通过命令行定义 C++ 预处理器宏?

        时间:2023-08-28

        <legend id='UugTE'><style id='UugTE'><dir id='UugTE'><q id='UugTE'></q></dir></style></legend>
          <tbody id='UugTE'></tbody>
          • <bdo id='UugTE'></bdo><ul id='UugTE'></ul>
              • <i id='UugTE'><tr id='UugTE'><dt id='UugTE'><q id='UugTE'><span id='UugTE'><b id='UugTE'><form id='UugTE'><ins id='UugTE'></ins><ul id='UugTE'></ul><sub id='UugTE'></sub></form><legend id='UugTE'></legend><bdo id='UugTE'><pre id='UugTE'><center id='UugTE'></center></pre></bdo></b><th id='UugTE'></th></span></q></dt></tr></i><div id='UugTE'><tfoot id='UugTE'></tfoot><dl id='UugTE'><fieldset id='UugTE'></fieldset></dl></div>

                <small id='UugTE'></small><noframes id='UugTE'>

                  <tfoot id='UugTE'></tfoot>
                  本文介绍了如何使用 CMake 通过命令行定义 C++ 预处理器宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我尝试在 CMake 的命令行中设置预处理器宏.我试过了:

                  I try to set a preprocessor macro in the command line of CMake. I've tried:

                  set generator="Visual Studio 8 2005"
                  set params=-D MY_MACRO=1
                  cmake.exe -G %generator% %params% ..some_project
                  

                  但它既没有在我编译时定义,也没有在 CMake 生成的文件中找到名称 MY_MACRO,除了 CMakeCache.txt 它存在于形式:

                  but it's neither defined when I compile nor can I find the name MY_MACRO in the files generated by CMake at all, except for CMakeCache.txt where it's present in the form:

                  MY_MACRO:UNINITIALIZED=1
                  

                  我该怎么做?

                  推荐答案

                  这个问题背后的动机是批量构建 3rd 方库,这就是我想避免修改 CMakeLists.txt 的原因.多年后,即使我不再需要它,我发现它很容易通过CMake 外部手段来实现:

                  The motivation behind the question was to batch build 3rd party libraries, which is why I wanted to avoid modifying CMakeLists. So years later, even though I don't need that anymore, I figured out that it's easily achievable by means external to CMake:

                  • 像往常一样调用 CMake,没有特殊标志.

                  • Invoke CMake as usual, no special flags.

                  那么:

                  • 使用 MSVC: 编译器读取 CL 环境变量以获得额外的命令行参数.所以

                  • With MSVC: The compiler reads the CL environment variable to get extra command line arguments. So

                    set CL=/DMY_MACRO=1 %CL%
                  

                  然后调用 MSBuild 来完成它的工作.

                  then invoke MSBuild to do its job.

                  使用 Makefile: 生成的 makefile 使用 CFLAGSCXX_FLAGS 变量作为 makefile 的预期用途.所以构建可以通过

                  With Makefiles: The generated makefiles use the CFLAGS and CXX_FLAGS variables as makefiles are expected to do. So the build can be started by

                    make CXX_FLAGS=-DMY_MACRO=1
                  

                  或者通过设置相应的环境变量.

                  or by setting the corresponding environment variables.

                  这篇关于如何使用 CMake 通过命令行定义 C++ 预处理器宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在跨平台 cmake 项目中设置编译器标志的现代方法 下一篇:对同一语言的多个编译器使用 CMake

                  相关文章

                    <bdo id='J2UbH'></bdo><ul id='J2UbH'></ul>
                1. <i id='J2UbH'><tr id='J2UbH'><dt id='J2UbH'><q id='J2UbH'><span id='J2UbH'><b id='J2UbH'><form id='J2UbH'><ins id='J2UbH'></ins><ul id='J2UbH'></ul><sub id='J2UbH'></sub></form><legend id='J2UbH'></legend><bdo id='J2UbH'><pre id='J2UbH'><center id='J2UbH'></center></pre></bdo></b><th id='J2UbH'></th></span></q></dt></tr></i><div id='J2UbH'><tfoot id='J2UbH'></tfoot><dl id='J2UbH'><fieldset id='J2UbH'></fieldset></dl></div>

                  <small id='J2UbH'></small><noframes id='J2UbH'>

                  <legend id='J2UbH'><style id='J2UbH'><dir id='J2UbH'><q id='J2UbH'></q></dir></style></legend>

                      <tfoot id='J2UbH'></tfoot>