docopt.cpp icon indicating copy to clipboard operation
docopt.cpp copied to clipboard

Add documentation on integrating docopt.cpp with a client CMake project

Open ghisvail opened this issue 9 years ago • 1 comments

The README is reasonably detailed as far as context and usage his concerned.

However, I believe it could be significantly enhanced by adding a specific section regarding the integration of docopt.cpp within a client CMake project. It should probably cover the following 3 use-cases:

  • directly vendoring the docopt.cpp source code.
  • using ExternalProject to download it as a pre-build command.
  • using find_package if a system copy is installed.

ghisvail avatar Dec 08 '16 08:12 ghisvail

I'm writing the specific suggestions below after finding this issue and having a hard time getting docopt to work with my cmake-compiled program (which includes the docopt source code in its own subfolder). I'm new to C++; I suspect that others like me would benefit from additional documentation on getting docopt linked correctly with an existing project.

To get docopt working with my project, I needed to add the following:

In my program's main.cpp file:

#include <docopt-0.6.2/docopt.h>

In my program's CMakeLists.txt file:

target_compile_definitions(projectname PRIVATE DOCOPT_HEADER_ONLY=1) # This is added because of an if statement at the bottom of docopt.h -- this enables actually including docopt.cpp from docopt.h.

set(CMAKE_CXX_FLAGS "-std=c++11") # docopt needs this, per https://github.com/Qihoo360/logkafka/issues/1

Thus, CMakeLists.txt looked like this:

cmake_minimum_required(VERSION 2.8)

PROJECT(projectname)

set(ROOT ./)

include_directories(${ROOT})

ADD_EXECUTABLE(projectname main.cpp marker.cpp page.cpp)

FIND_PACKAGE(OpenCV REQUIRED)

TARGET_LINK_LIBRARIES(projectname ${OpenCV_LIBS})

##############
# For getting docopt to work
##############

target_compile_definitions(projectname PRIVATE DOCOPT_HEADER_ONLY=1) # This is added because of an if statement at the bottom of docopt.h -- this enables actually including docopt.cpp from docopt.h.
set(CMAKE_CXX_FLAGS "-std=c++11") # docopt needs this, per https://github.com/Qihoo360/logkafka/issues/1

##############

FILE(COPY ${PROJECT_SOURCE_DIR}/Example_Images/test_input.jpg    DESTINATION ${PROJECT_BINARY_DIR}/bin)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)

jglev avatar Feb 07 '17 19:02 jglev