CMake and SWIG are now quite famous, but you can easily generate C / C ++ glue code by combining the two.
Sorry for the shabby content to conclude Advent Calendar 2013 ...
CMakeList.txt
find_package(PythonLibs REQUIRED)
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})
include(GenerateExportHeader)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${PROJECT_BINARY_DIR}
${PYTHON_INCLUDE_PATH}
)
set(EXAM_SRCS
example.h
)
set(INTERFACE_FILES
example.i
)
set_source_files_properties(${INTERFACE_FILES} PROPERTIES CPLUSPLUS ON)
swig_add_module(example python ${INTERFACE_FILES}
${EXAM_SRCS}
)
swig_link_libraries(example
${PYTHON_LIBRARIES}
)
example.h
#pragma once
#include <string>
class ExampleClass{
std::string m_msg;
public:
ExampleClass() : m_msg("Hello World") { }
const std::string& get_msg(){ return m_msg; }
void set_msg(const std::string &msg){ m_msg = msg; }
};
example.i
%module example
%include "std_string.i"
%{
#define SWIG_FILE_WITH_INIT
%}
%{
#include "example.h"
%}
%include "example.h"
$cd build && cmake .. && make
$python
>>> import example
>>> e = example.ExampleClass()
>>> e.get_msg()
'Hello World'
>>> e.set_msg("FizzBuzz")
>>> e.get_msg()
'FizzBuzz'
You can use it by specifying another language in CMakeLists.txt.
Recommended Posts