CMake und SWIG sind mittlerweile ziemlich berühmt, aber Sie können einfach C / C ++ - Klebercode generieren, indem Sie diese beiden kombinieren.
Entschuldigung für den schäbigen Inhalt zum Abschluss des Adventskalenders 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'
Sie können es verwenden, indem Sie in CMakeLists.txt eine andere Sprache angeben.
Recommended Posts