简体   繁体   English

CMake如何配置这个C ++项目?

[英]CMake how to configure this C++ project?

I want a cmake project with two configurations BUILD and TEST. 我想要一个具有两个配置BUILD和TEST的cmake项目。

BUILD compiles all sources not in test subdirectory into a shared library. BUILD将不在测试子目录中的所有源代码编译到共享库中。 TEST compiles all sources, including those in test subdirectory (which includes main.cpp) into an executable that runs the tests. TEST将所有源(包括测试子目录(包括main.cpp)中的源)编译为运行测试的可执行文件。 I don't want TEST to build the shared library. 我不想测试建立共享库。 I don't want BUILD to build the test executable. 我不希望BUILD构建测试可执行文件。

I currently have it on disk as: 我目前在磁盘上将其作为:

project/
  test/
    test_foo.cpp
    main.cpp

  bar.hpp
  widget.hpp
  bar.cpp
  widget.cpp
  ...

I can move things around if it makes it easier. 如果可以简化的话,我可以四处移动。 What do I put in my CMakeLists.txt files? 我在CMakeLists.txt文件中放什么?

It looks to me like you want to use cmake's OPTION command. 在我看来,您想要使用cmake的OPTION命令。 Pick one configuration to be on by default (or neither if you want to force the person compiling the code to choose) 选择一种默认情况下处于启用状态的配置(或者,如果您想强迫编译代码的人选择该选项,则两者都不选)

OPTION( BUILD_SHARED_LIBRARY "Compile sources into shared library" ON )
OPTION( RUN_TESTS "Compile test executable and run it" OFF )

You'll want to make sure that the options are mutually exclusive, and error out otherwise 您需要确保这些选项互斥,否则会出错

if ( BUILD_SHARED_LIBRARY AND RUN_TESTS )
  message(FATAL_ERROR "Can't build shared library and run tests at same time")
endif()

You can then put the rest of your commands inside if blocks based on those variables 然后,您可以将其余命令放到基于这些变量的块中

if ( BUILD_SHARED_LIBRARY )
  #add subdirectories except for test, add sources to static library, etc
endif()

if ( RUN_TESTS )
  #compile an executable and run it, etc.
endif()

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

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