Last active
June 17, 2021 13:41
-
-
Save ibraEssam/68ba35d45d6f357004fc73cfdbb7fdbd to your computer and use it in GitHub Desktop.
Revisions
-
ibraEssam revised this gist
Jun 17, 2021 . No changes.There are no files selected for viewing
-
ibraEssam revised this gist
Jun 17, 2021 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -61,3 +61,10 @@ int main(int argc, char const *argv[]) { ### Make sure that the shared library is in your `LD_LIBRARY_PATH` `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${pwd}` ### Run `./main` >> HI from C++ >> 80 -
ibraEssam revised this gist
Jun 17, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ TestCPP.hpp ```c++ #ifndef __TESTCPP_H__ -
ibraEssam created this gist
Jun 17, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,63 @@ ` TestCPP.hpp ```c++ #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 ```c++ #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 ```c #include "TestCPP.hpp" int main(int argc, char const *argv[]) { sayTestCpp(); EigenTest(); return 0; } ``` ### Build C++ code as a shared Library `g++ -fpic -shared TestCPP.cpp -o libtest_cpp.so` ### Build C code and link it to the shared C++ library `gcc main.c -L. -ltest_cpp -o main` ### Make sure that the shared library is in your `LD_LIBRARY_PATH` `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${pwd}`