Skip to content

Instantly share code, notes, and snippets.

@nschloe
Created September 1, 2016 21:50
Show Gist options
  • Save nschloe/5e213272671db5dd745508093f37a4a1 to your computer and use it in GitHub Desktop.
Save nschloe/5e213272671db5dd745508093f37a4a1 to your computer and use it in GitHub Desktop.
SWIG with Eigen3
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}
)
#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
%module mytest
%{
#define SWIG_FILE_WITH_INIT
#include "mytest.hpp"
%}
%include "mytest.hpp"
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