簡體   English   中英

CMake如何配置這個C ++項目?

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

我想要一個具有兩個配置BUILD和TEST的cmake項目。

BUILD將不在測試子目錄中的所有源代碼編譯到共享庫中。 TEST將所有源(包括測試子目錄(包括main.cpp)中的源)編譯為運行測試的可執行文件。 我不想測試建立共享庫。 我不希望BUILD構建測試可執行文件。

我目前在磁盤上將其作為:

project/
  test/
    test_foo.cpp
    main.cpp

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

如果可以簡化的話,我可以四處移動。 我在CMakeLists.txt文件中放什么?

在我看來,您想要使用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 ( 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