Skip to content

Instantly share code, notes, and snippets.

@ibraEssam
Last active June 17, 2021 13:41
Show Gist options
  • Select an option

  • Save ibraEssam/68ba35d45d6f357004fc73cfdbb7fdbd to your computer and use it in GitHub Desktop.

Select an option

Save ibraEssam/68ba35d45d6f357004fc73cfdbb7fdbd to your computer and use it in GitHub Desktop.

Revisions

  1. ibraEssam revised this gist Jun 17, 2021. No changes.
  2. ibraEssam revised this gist Jun 17, 2021. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions eigen_in_c.md
    Original 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
  3. ibraEssam revised this gist Jun 17, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion eigen_in_c.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    `

    TestCPP.hpp
    ```c++
    #ifndef __TESTCPP_H__
  4. ibraEssam created this gist Jun 17, 2021.
    63 changes: 63 additions & 0 deletions eigen_in_c.md
    Original 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}`