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).
This commit is contained in:
Mario Badr 2016-12-03 07:44:46 +11:00 committed by Jarryd Beck
parent f6bd09df63
commit e005d07656
5 changed files with 21 additions and 12 deletions

View File

@ -26,7 +26,7 @@ RETURN()
ENDIF()
cmake_minimum_required(VERSION 2.8.12)
cmake_minimum_required(VERSION 3.1)
project(cxxopts)
enable_testing()
@ -37,9 +37,7 @@ option(CXXOPTS_BUILD_EXAMPLES "Set to ON to build examples" ON)
option(CXXOPTS_BUILD_TESTS "Set to ON to build tests" OFF)
set(CXXOPTS_LINKER_LIBRARIES "")
set(CXXOPTS_USE_UNICODE_HELP FALSE CACHE BOOL "Use ICU Unicode library")
if(CXXOPTS_USE_UNICODE_HELP)
find_package(PkgConfig)
@ -48,8 +46,23 @@ if(CXXOPTS_USE_UNICODE_HELP)
set(CXXOPTS_LINKER_LIBRARIES "${ICU_LDFLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ICU_CFLAGS} -DCXXOPTS_USE_UNICODE")
endif()
add_library(cxxopts INTERFACE)
target_sources(
cxxopts INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/include/cxxopts.hpp
)
target_include_directories(
cxxopts INTERFACE
include/
)
target_link_libraries(
cxxopts
INTERFACE ${CXXOPTS_LINKER_LIBRARIES}
)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/cxxopts.hpp DESTINATION include)
add_subdirectory(src)
add_subdirectory(test)

View File

@ -19,9 +19,8 @@
# THE SOFTWARE.
if(CXXOPTS_BUILD_EXAMPLES)
add_executable(example example.cpp cxxopts.hpp)
target_link_libraries(example ${CXXOPTS_LINKER_LIBRARIES})
add_executable(example example.cpp)
target_link_libraries(example cxxopts)
if (MSVC)
target_compile_options(example PUBLIC /W2)
@ -29,5 +28,3 @@ if(CXXOPTS_BUILD_EXAMPLES)
target_compile_options(example PUBLIC -std=c++11 -Wall)
endif()
endif()
install(FILES cxxopts.hpp DESTINATION include)

View File

@ -1,7 +1,6 @@
if (CXXOPTS_BUILD_TESTS)
add_executable(options_test options.cpp)
target_link_libraries(options_test ${CXXOPTS_LINKER_LIBRARIES})
target_link_libraries(options_test cxxopts)
if (MSVC)
target_compile_options(options_test PUBLIC /W2)

View File

@ -3,7 +3,7 @@
#include <initializer_list>
#include "../src/cxxopts.hpp"
#include "cxxopts.hpp"
class Argv {
public: