Cmake-2
target_include_directories(Tutorial PUBLIC"${PROJECT_BINARY_DIR}")
把配置文件寫到二進(jìn)制樹中。
?
# specify the C++ standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True)上面的命令指定C++的標(biāo)準(zhǔn)。
Step 2:增加一個(gè)庫
把自定義的庫文件放到子目錄:MathFunctions;庫包含頭文件:MathFunctions.h和源文件:mysqrt.cxx;有一個(gè)方法是mysqrt。
add_library(MathFunctions mysqrt.cxx)的cmakeLists.txt到MathFunctions的子目錄中。為了使用mathFunctions的功能,高一級(jí)的cmakeLists的寫法如下:
# add the MathFunctions library add_subdirectory(MathFunctions)# add the executable add_executable(Tutorial tutorial.cxx)target_link_libraries(Tutorial PUBLIC MathFunctions)# add the binary tree to the search path for include files # so that we will find TutorialConfig.h target_include_directories(Tutorial PUBLIC"${PROJECT_BINARY_DIR}""${PROJECT_SOURCE_DIR}/MathFunctions")為了讓這個(gè)math庫變?yōu)榭膳渲玫摹?/p> option(USE_MYMATH "Use tutorial provided math implementation" ON)# configure a header file to pass some of the CMake settings # to the source code configure_file(TutorialConfig.h.in TutorialConfig.h)
改變上一級(jí)的cmakelist.txt:
if(USE_MYMATH)add_subdirectory(MathFunctions)list(APPEND EXTRA_LIBS MathFunctions)list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions") endif()# add the executable add_executable(Tutorial tutorial.cxx)target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})# add the binary tree to the search path for include files # so that we will find TutorialConfig.h target_include_directories(Tutorial PUBLIC"${PROJECT_BINARY_DIR}"${EXTRA_INCLUDES})在?in?tutorial.cxx中使用宏定義:
#ifdef USE_MYMATH # include "MathFunctions.h" #endif #ifdef USE_MYMATHconst double outputValue = mysqrt(inputValue); #elseconst double outputValue = sqrt(inputValue); #endif編譯:
cmake ../Step2 -DUSE_MYMATH=OFFStep 3:添加庫的使用要求
1)接口的使用,是用戶需要的,需要修改MathFunctions/CMakeLists.txt增加下面一行:
?
target_include_directories(MathFunctionsINTERFACE ${CMAKE_CURRENT_SOURCE_DIR})如果增加了上面一行,那就可以修改上一級(jí)的CMakeLists.txt:
if(USE_MYMATH)add_subdirectory(MathFunctions)list(APPEND EXTRA_LIBS MathFunctions) endif()And here:
target_include_directories(Tutorial PUBLIC"${PROJECT_BINARY_DIR}")?
?
?
總結(jié)