TestCPP.hpp
#ifndef __TESTCPP_H__
#define __TESTCPP_H__
#ifdef __cplusplus
extern "C" {
#endif
void sayTestCpp(); // Print Hi from C++
void EigenTest(); // use a simple Eigen arithmetics.
#ifdef __cplusplus
} // extern c
#endif // cplusplus
#endif // __TESTCPP_H__
TestCPP.cpp
#include "TestCPP.hpp"
#include <eigen3/Eigen/Dense>
#include <iostream>
extern "C" {
void sayTestCpp() { std::cout << "HI from C++" << std::endl; }
void EigenTest() {
Eigen::Vector2d x = {12, 34};
auto y = x.dot(Eigen::Vector2d(1, 2).transpose());
std::cout << y << std::endl;
}
} // extern c
main.c
#include "TestCPP.hpp"
int main(int argc, char const *argv[]) {
sayTestCpp();
EigenTest();
return 0;
}
g++ -fpic -shared TestCPP.cpp -o libtest_cpp.so
gcc main.c -L. -ltest_cpp -o main
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${pwd}
./main
HI from C++
80