here's my source code directory structure.
some project
libs
mylib1
...
3rdlibs1
...
apps
myapp1
...
My requirments are as follows:
Can you give me some sample code. I know ExternalProject_Add can help me to compile 3rdlibs. But I really don't know how to do it. I think other people may also be interesting to this question.
If you have all your sources in single file system tree, it is better to use add_subdirectory
than ExternalProject
. ExternalProject
is for projects which are truly external, eg on remote server or VCS repository. Unlike add_subdirectory
which can create target of any type, ExternalProject
can create only UTILITY target, similar to add_custom_target
command. UTILITY targets have limitations, eg you can not use them in target_link_libraries
command. Using add_subdirectory
is much simpler. Top-level:
cmake_minimum_required(VERSION 3.7)
project("some_project")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_subdirectory(libs/3rdlibs1)
add_subdirectory(libs/mylib1)
add_subdirectory(apps/myapp1)
App:
project("myapp1")
set(SRC_FILES ...)
add_executable(myapp1 ${SRC_FILES})
target_link_libraries(myapp1 PRIVATE mylib1)
Lib. As I understand from your description, 3rdlibs1 is optional dependency of mylib1. Otherwise, how can myapp use mylib1 without 3rdlibs1?
project("mylib1")
option(THIRD_LIBS_SUPPORT "description" OFF)
set(SRC_FILES ...)
add_library(mylib1 STATIC ${SRC_FILES})
#PUBLIC means that both mylib1 and its dependents use the headers
target_include_directories(mylib1 PUBLIC "${PROJECT_SOURCE_DIR}/include")
if(THIRD_LIBS_SUPPORT)
#PUBLIC means that 3rdlibs1 will be linked to mylib1 dependents
target_link_libraries(mylib1 PUBLIC 3rdlibs1)
target_compile_definitions(mylib1 PUBLIC -DTHIRD_LIBS_SUPPORT)
endif()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.