banner
cells

cells

为美好的世界献上 bug

CMake 記錄

對於一個專案結構:

.
├── CMakeLists.txt
├── func
   ├── CMakeLists.txt
   ├── include
   └── func.h
   ├── lib
   └── libfunc_lib.a
   └── src
       └── func.cpp
└── main.cpp

子模組的 CMakeLists.txt 檔案:

# ./func/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)

# project(func_lib LANGUAGES CXX VERSION 1.0)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

set(func_src
    ${CMAKE_CURRENT_SOURCE_DIR}/src/func.cpp
)

# add_library(${PROJECT_NAME} SHARED ${func_src})
# add_library(${PROJECT_NAME} STATIC ${func_src})
add_library(func_lib STATIC ${func_src})

# 指定庫的頭檔
# target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(func_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)

專案的根目錄 CMakeLists.txt 檔案:

# ./CMakeLists.txt
cmake_minimum_required(VERSION 3.10)

project(main LANGUAGES CXX VERSION 1.0)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory(${PROJECT_SOURCE_DIR}/func)

set(src
    ${PROJECT_SOURCE_DIR}/main.cpp
)

add_executable(${PROJECT_NAME} ${src})

target_link_libraries(${PROJECT_NAME} PRIVATE func_lib)

對於子模組的 CMakeLists.txt 中不包含 project() 命令時,PROJECT_SOURCE_DIR 變數代表的是專案根目錄:

image

對於子模組的 CMakeLists.txt 中當有 project() 命令時,PROJECT_SOURCE_DIR 變數代表的是當前包含 project() 的 CMakeLists.txt 所在的目錄:

2024-10-02-15-53

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。