For a project structure:
.
├── CMakeLists.txt
├── func
│ ├── CMakeLists.txt
│ ├── include
│ │ └── func.h
│ ├── lib
│ │ └── libfunc_lib.a
│ └── src
│ └── func.cpp
└── main.cpp
CMakeLists.txt file for the submodule:
# ./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})
# Specify the library's header files
# target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(func_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
CMakeLists.txt file for the project's root directory:
# ./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)
When the CMakeLists.txt of the submodule does not contain the project()
command, the PROJECT_SOURCE_DIR
variable represents the root directory of the project:
When the CMakeLists.txt of the submodule contains the project()
command, the PROJECT_SOURCE_DIR
variable represents the directory where the CMakeLists.txt containing project()
is located: