Generating SWIG bindings with CMake

2019-02-17 01:13发布

问题:

How would I generate automatic bindings for a C project that is built using CMake?

I want to generate bindings for Python, Java, .NET, PHP, Perl, TCL, Ruby and Octave automatically.

回答1:

You can find an example here.

Snippet:

The following example is a CMake input file for creating a python wrapper for the SWIG interface file, example.i:

# This is a CMake example for Python

FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})

FIND_PACKAGE(PythonLibs)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

SET(CMAKE_SWIG_FLAGS "")

SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES SWIG_FLAGS "-includeall")
SWIG_ADD_MODULE(example python example.i example.cxx)
SWIG_LINK_LIBRARIES(example ${PYTHON_LIBRARIES})