简体   繁体   English

CMake - 将选项集成到 C++ 源文件中

[英]CMake - integrating options into C++ source files

I'm working with an existing project and cleaning up the CMake for it.我正在处理一个现有项目并为其清理 CMake。 However, right now I'm a bit confused by how exactly to go about integrating the CMake options into the actual source code.但是,现在我对 go 如何将 CMake 选项集成到实际源代码中感到有点困惑。

For simplicity's sake, let's say I'd like to only execute one chunk of code, let's say cout << "print";为简单起见,假设我只想执行一段代码,假设cout << "print"; , inside example.cpp if, on CMake, the value ENABLE_PRINT is set to ON . ,在example.cpp中,如果在 CMake 上,值ENABLE_PRINT设置为ON

The project directory will look like this:项目目录将如下所示:文件夹布局

Using the above example, I did the following:使用上面的示例,我执行了以下操作:

  1. On the parent project CMakeLists.txt , I added OPTION( ENABLE_PRINT "Enable Print" ON)父项目CMakeLists.txt上,我添加了OPTION( ENABLE_PRINT "Enable Print" ON)
  2. Then, on the example subproject source folder Config.h file, I added #define ENABLE_PRINT然后,在示例子项目源文件夹Config.h文件中,我添加了#define ENABLE_PRINT
  3. On the Config.h.in located in the example subproject , I added #cmakedefine ENABLE_PRINT在位于示例子项目Config.h.in中,我添加了#cmakedefine ENABLE_PRINT
  4. Finally, on the source file example.cpp , I encircled cout << "print";最后,在源文件example.cpp上,我圈出了cout << "print"; inside #ifdef ENABLE_PRINT and #endif#ifdef ENABLE_PRINT#endif

After making those changes, the project will configure and generate just fine.进行这些更改后,项目将配置并生成得很好。 However, when I make the software, it will error and essentially tell me that the chunk of code I encircled with #ifdef was not executed at all;但是,当我制作软件时,它会出错,基本上告诉我用#ifdef包围的代码块根本没有执行; it was ignored.它被忽略了。 In other words, the above steps I took did not do anything except "comment out" the chunk of code I wanted to make conditional upon ENABLE_PRINT.换句话说,除了“注释掉”我想以 ENABLE_PRINT 为条件的代码块之外,我采取的上述步骤没有做任何事情。

So, how would I make this work?那么,我将如何进行这项工作?

You may combine option and add_definitions of cmake like here . 您可以像这里一样组合cmake的optionadd_definitions Since a simple example is clearer than a long text here is a little main.c : 由于一个简单的例子比一个长文本更清晰,这里有一点main.c:

 #include<stdio.h>

 int main(int argc, char *argv[])
 {
   printf("start\n");
   #ifdef USE_DEBUG
     printf("Using debug\n");
   #endif
   printf("end\n");
   return 0;
 }

The CMakeLists.txt is : CMakeLists.txt是:

cmake_minimum_required (VERSION 2.6)
project (HELLO) 

option(WITH_DEBUG "Use debug" OFF)

if (WITH_DEBUG)
  MESSAGE(STATUS "WITH_DEBUG")
  add_definitions(-DUSE_DEBUG)
endif()

add_executable (main main.c) 

You can try it by typing cmake . 你可以输入cmake .来试试cmake . or cmake . -DWITH_DEBUG=ON cmake . -DWITH_DEBUG=ON cmake . -DWITH_DEBUG=ON then make and ./main cmake . -DWITH_DEBUG=ON然后make./main

@francis answer is good, however I would also recommend using compile_definitions instead of add_definitions as well as specifying target for modern cmake modularity: @francis 的回答很好,但是我也建议使用 compile_definitions 而不是 add_definitions 以及为现代 cmake 模块化指定目标:

cmake_minimum_required (VERSION 2.6)
project (HELLO) 

option(WITH_DEBUG "Use debug" OFF)

add_executable (main main.c) 

if (WITH_DEBUG)
  MESSAGE(STATUS "WITH_DEBUG")
  target_compile_definitions(main PRIVATE USE_DEBUG)
endif()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM