Reputation: 157
I want to give examples to some users of my project. The exmaple is to be used CMake. And It's like:
- top level
CMakeLists.txt
-- example A
A.cpp
CMakeLists.txt
-- example B
B.cpp
CMakeLists.txt
-- example C
C.cpp
CMakeLists.txt
I want to set some common CMake options in the top CMakeLists.txt, like:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -fexperimental-library")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lc++abi -fuse-ld=lld")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -lc++abi -fuse-ld=lld")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -lc++abi -fuse-ld=lld")
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
And I want that the example project can be built one time and the sub example can be built independently.
#A.cpp
cmake_minimum_required(VERSION 3.24)
# Something to be added
project(A)
add_executable(${PROJECT_NAME} A.cpp)
But I wish that I can do it without copy the common options into each CMakeLists.txt. I don't find a simple answer on StackOverflow or throughout Google. I think it should be a question may be met frequently in the fact. It's helpful to provide a tutorial even just a tutorial url.
Upvotes: 1
Views: 241
Reputation: 96
Simply use add_subdirectory
? Would simplify things and should still meet your requirement?
option(MYPROJECT_BUILD_EXAMPLES "Build examples" OFF/ON)
...
if (MYPROJECT_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
Upvotes: 2