Exercise 1 - Creating a Library
1.使用add_library(MathFunctions MathFunctions.cxx mysqrt.cxx)
建立lib
2.add_subdirectory(MathFunctions)
包含子目录
3.target_link_libraries(Tutorial PUBLIC MathFunctions)
链接
4.target_include_directories(Tutorial PUBLIC ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}/MathFunctions)
包含链接库头文件
总结:
建立链接库add_library
添加链接库:target_link_libraries target_include_directories
Exercise 2 - Adding an Option
1.定义:
option(USE_MYMATH "Use tutorial provided math implementation" ON)
2.if:
if(USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
add_library(SqrtLibrary STATIC
mysqrt.cxx
)
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif()
其中 target_compile_definitions
定义可被#ifdefine访问
评论区