Created
September 1, 2016 21:50
-
-
Save nschloe/5e213272671db5dd745508093f37a4a1 to your computer and use it in GitHub Desktop.
SWIG with Eigen3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.0) | |
project(mytest) | |
FIND_PACKAGE(SWIG REQUIRED) | |
FIND_PACKAGE(Eigen3 REQUIRED) | |
INCLUDE(${SWIG_USE_FILE}) | |
if (NOT MSVC) | |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | |
endif() | |
FIND_PACKAGE(PythonLibs 2 REQUIRED) | |
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH}) | |
INCLUDE_DIRECTORIES(${EIGEN3_INCLUDE_DIR}) | |
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) | |
SET_SOURCE_FILES_PROPERTIES(mytest.i PROPERTIES CPLUSPLUS ON) | |
SWIG_ADD_MODULE(mytest python mytest.i) | |
SWIG_LINK_LIBRARIES( | |
mytest | |
${PYTHON_LIBRARIES} | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MYTEST_HPP | |
#define MYTEST_HPP | |
#include <iostream> | |
#include <Eigen/Dense> | |
void | |
print_norm(const Eigen::Vector3d & x) { | |
std::cout << x.norm() << std::endl; | |
} | |
void | |
print_norms(const std::vector<Eigen::Vector3d> & xs) { | |
for (const auto & x: xs) { | |
std::cout << x.norm() << std::endl; | |
} | |
} | |
#endif // MYTEST_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%module mytest | |
%{ | |
#define SWIG_FILE_WITH_INIT | |
#include "mytest.hpp" | |
%} | |
%include "mytest.hpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mytest | |
a = [1, 1, 0] | |
mytest.print_norm(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment