Commit e005d076568ea7a6d1979a8f80a988728e136d5a
Committed by
Jarryd Beck
1 parent
f6bd09df
Use cmake interface library
Fixes #33. This PR uses cmake's interface library feature: An INTERFACE library target does not directly create build output, though it may have properties set on it and it may be installed, exported and imported. This makes it easier to include the header only library in a cmake project. After using add_subdirectory on the cxxopts directory, one simply needs to include cxxopts in their target_link_libraries, which will allow the user's target to inherit the properties of the cxxopts header library (see changes to example and test).
Showing
5 changed files
with
21 additions
and
12 deletions
CMakeLists.txt
| ... | ... | @@ -26,7 +26,7 @@ RETURN() |
| 26 | 26 | |
| 27 | 27 | ENDIF() |
| 28 | 28 | |
| 29 | -cmake_minimum_required(VERSION 2.8.12) | |
| 29 | +cmake_minimum_required(VERSION 3.1) | |
| 30 | 30 | project(cxxopts) |
| 31 | 31 | |
| 32 | 32 | enable_testing() |
| ... | ... | @@ -37,9 +37,7 @@ option(CXXOPTS_BUILD_EXAMPLES "Set to ON to build examples" ON) |
| 37 | 37 | option(CXXOPTS_BUILD_TESTS "Set to ON to build tests" OFF) |
| 38 | 38 | |
| 39 | 39 | set(CXXOPTS_LINKER_LIBRARIES "") |
| 40 | - | |
| 41 | 40 | set(CXXOPTS_USE_UNICODE_HELP FALSE CACHE BOOL "Use ICU Unicode library") |
| 42 | - | |
| 43 | 41 | if(CXXOPTS_USE_UNICODE_HELP) |
| 44 | 42 | |
| 45 | 43 | find_package(PkgConfig) |
| ... | ... | @@ -48,8 +46,23 @@ if(CXXOPTS_USE_UNICODE_HELP) |
| 48 | 46 | |
| 49 | 47 | set(CXXOPTS_LINKER_LIBRARIES "${ICU_LDFLAGS}") |
| 50 | 48 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ICU_CFLAGS} -DCXXOPTS_USE_UNICODE") |
| 51 | - | |
| 52 | 49 | endif() |
| 53 | 50 | |
| 51 | +add_library(cxxopts INTERFACE) | |
| 52 | +target_sources( | |
| 53 | + cxxopts INTERFACE | |
| 54 | + ${CMAKE_CURRENT_SOURCE_DIR}/include/cxxopts.hpp | |
| 55 | + ) | |
| 56 | +target_include_directories( | |
| 57 | + cxxopts INTERFACE | |
| 58 | + include/ | |
| 59 | + ) | |
| 60 | +target_link_libraries( | |
| 61 | + cxxopts | |
| 62 | + INTERFACE ${CXXOPTS_LINKER_LIBRARIES} | |
| 63 | + ) | |
| 64 | + | |
| 65 | +install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/cxxopts.hpp DESTINATION include) | |
| 66 | + | |
| 54 | 67 | add_subdirectory(src) |
| 55 | 68 | add_subdirectory(test) | ... | ... |
src/cxxopts.hpp renamed to include/cxxopts.hpp
src/CMakeLists.txt
| ... | ... | @@ -19,9 +19,8 @@ |
| 19 | 19 | # THE SOFTWARE. |
| 20 | 20 | |
| 21 | 21 | if(CXXOPTS_BUILD_EXAMPLES) |
| 22 | - add_executable(example example.cpp cxxopts.hpp) | |
| 23 | - | |
| 24 | - target_link_libraries(example ${CXXOPTS_LINKER_LIBRARIES}) | |
| 22 | + add_executable(example example.cpp) | |
| 23 | + target_link_libraries(example cxxopts) | |
| 25 | 24 | |
| 26 | 25 | if (MSVC) |
| 27 | 26 | target_compile_options(example PUBLIC /W2) |
| ... | ... | @@ -29,5 +28,3 @@ if(CXXOPTS_BUILD_EXAMPLES) |
| 29 | 28 | target_compile_options(example PUBLIC -std=c++11 -Wall) |
| 30 | 29 | endif() |
| 31 | 30 | endif() |
| 32 | - | |
| 33 | -install(FILES cxxopts.hpp DESTINATION include) | ... | ... |
test/CMakeLists.txt