CMake 学习笔记整理(7)

把笔记分享给大家!
接上篇内容 CMake 学习笔记整理(6)

######################t17
# 之前的笔记t9, install(FILES xyz.h DESTINATION include/xyz)
# proj2/src
#     main.cpp
    #include 

    int main(){
        china tianchao;
        tianchao.Foo();
        return 0;
    }

# CMake 被设计成了可扩展的架构, 可以通过编写一些通用的模块来扩展CMake
# 添加模块路径
# 新工程目录:
# proj2/
#   CMakeLists.txt
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
add_subdirectory(src)

# NOTE:
# 不要把这两行写反!

# find_package(): 于之相对的文件是 Find
# 就是这个 proj2/cmake/FindCBA.cmake
# proj2/src
#      CMakeLists.txt
find_package(CBA)

set(CMAKE_LOOSE_LOOP_CONSTRUCTS ON)
if(CBA_FOUND)
    add_executable(hello main.cpp)
    include_directories(${XYZ_INCLUDE_DIR})
    target_link_libraries(hello ${CBA_LIBRARY})
endif()

# 这次依旧使用我们已经编译好的libcba.so 和xyz.h
# 这个是.cmake 文件!
# proj2/
#   FindCBA.cmake
find_path(XYZ_INCLUDE_DIR xyz.h /usr/include/xyz)
find_library(CBA_LIBRARY 
    NAMES 
        cba
    PATHS 
        /usr/lib
)

set(CMAKE_LOOSE_LOOP_CONSTRUCTS ON)
if(XYZ_INCLUDE_DIR AND CBA_LIBRARY)
    set(CBA_FOUND true)
endif()

if(CBA_FOUND)
# find_package(CBA QUIET) 将不会执行下面
    if(NOT CBA_FOUND_QUIETLY)
        message(STATUS "Found cba: ${CBA_LIBRARY}")
    endif()
else()
    if(CBA_FIND_REQUIRED)
        message(FATAL_ERROR "Could not find cba library")
    endif()
endif()

# cmake配置, make编译, 先清除无关和已配置和已编译的文件
# proj2/build
cmake ..
make

# result 完美执行 proj2/src/hello  output china::Foo
-- Found cba: /usr/lib/libcba.so
...
[100%] Building CXX object src/CMakeFiles/hello.dir/main.o
Linking CXX executable hello
[100%] Built target hello

# comment:
# 不懂就查 :-D
# https://cmake.org/cmake/help/v3.0/genindex.html#F

你可能感兴趣的:(CMake,学习笔记整理,cmake)