10

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

1 Answer 1

12

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})
Sign up to request clarification or add additional context in comments.

1 Comment

Beginning with CMake 3.8 it's SWIG_ADD_LIBRARY rather than SWIG_ADD_MODULE. The CMake documentation is probably better than the SWIG documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.