docopt / docopt.cpp

C++11 port of docopt
Boost Software License 1.0
1.05k stars 146 forks source link

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

Open ghisvail opened 7 years ago

ghisvail commented 7 years ago

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:

jglev commented 7 years ago

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)