【问题标题】:CMake how to configure this C++ project?CMake如何配置这个C++项目?
【发布时间】:2015-08-01 20:32:26
【问题描述】:

我想要一个具有 BUILD 和 TEST 两种配置的 cmake 项目。

BUILD 将不在 test 子目录中的所有源编译到共享库中。 TEST 将所有源代码(包括 test 子目录(包括 main.cpp)中的源代码)编译为运行测试的可执行文件。我不希望 TEST 构建共享库。我不想 BUILD 构建测试可执行文件。

我目前在磁盘上作为:

project/
  test/
    test_foo.cpp
    main.cpp

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

如果它更容易,我可以移动东西。我在我的 CMakeLists.txt 文件中放了什么?

【问题讨论】:

  • @JoachimPileborg 他在问他的 CMakeLists.txt 将如何寻找他想要的配置,这是如何基于意见的?
  • 您可能需要编辑您的问题以使其更清楚您想要什么,尤其是更改标题。
  • 我认为 Joachim 的意思类似于他的问题旨在“做我的工作”。 “我要放入什么......”是一种不好的提问方式。

标签: c++ cmake


【解决方案1】:

在我看来你想使用 cmake 的 OPTION 命令。选择一个默认开启的配置(或者如果你想强制编译代码的人选择不开启)

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

您需要确保选项是互斥的,否则会出错

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

然后,您可以根据这些变量将其余命令放入 if 块中

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()

【讨论】:

  • 谢谢!根据这个例子,我设法让事情正常进行。
猜你喜欢
  • 1970-01-01
  • 2022-01-06
  • 2018-11-18
  • 1970-01-01
  • 1970-01-01
  • 2018-03-16
  • 2019-07-09
  • 1970-01-01
相关资源
最近更新 更多