Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Eigen in a C Project

Tags:

c++

c

include

eigen

I am working on a C project I got from the Internet, and I'm trying to add some functions to the project that involve linear algebra. In my previous works in C++, I usually rely on Eigen for linear algebra.

Is there a way to use Eigen for a C project? If yes, what should I do to make that work? (Simply adding Eigen header files is not enough since for example the standard C++ files do not get included automatically)

like image 823
Beginner Avatar asked Nov 12 '14 14:11

Beginner


People also ask

What is Eigen template in C++?

Eigen is a C++ template library for linear algebra: matrices, vectors, numerical solvers, and related algorithms. This tutorial will get you through the steps of how to use Eigen in your own CMake project on Windows 10. These instructions will get you a full Eigen3 CMake template for your C++ project.

Can I use Eigen in CMake?

Using Eigen in CMake Projects Eigen provides native CMake support which allows the library to be easily used in CMake projects.

Can I use Eigen library in C++?

However, you can call functions implemented in C++ from C. So you can have a C++ part which uses Eigen and exposes a C interface, which the C portion of the project uses. Eigen is a library which heavily uses C++ features which are not present in C. As such, it cannot be directly used from a C translation unit.

Is it possible to use Eigen in a C-only project?

sjdowling is right that you cannot directly use Eigen in a C-only project. However, you can call functions implemented in C++ from C. So you can have a C++ part which uses Eigen and exposes a C interface, which the C portion of the project uses. Eigen is a library which heavily uses C++ features which are not present in C.


1 Answers

Eigen is a library which heavily uses C++ features which are not present in C. As such, it cannot be directly used from a C translation unit.

However, you can wrap the parts using Eigen in a separate shared library and expose a C interface. Here is a small example how one could write such a library.

Library interface

/* foo.h */

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

void foo(int arg);

#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */

By default, C++ uses different mangling rules than C for names of exported functions. We use extern "C" to instruct the C++ compiler to use the C rules. Because the interface file will be seen by both the C++ and the C compiler, we wrap the extern declaration in #ifdefs which will only trigger for the C++ compiler.

Library implementation

/* foo.cpp */

#include "foo.h"

#include <iostream>

extern "C" {

void foo(int arg) {
  std::cout << arg << std::endl;
}

} /* extern "C" */

We also need to define C linkage in the definition of the interface. Other than that, you can use any C++ features you like in the implementation (including Eigen).

C project

/* main.c */

#include "foo.h"

int main() {
  foo(42);
  return 0;
}

Include the interface header and use it like any other C library.

Building

$ g++ foo.cpp -shared -fPIC -o libfoo.so
$ gcc main.c -L. -lfoo -o main

Use a C++ compiler to build the shared library libfoo.so. Use a C compiler to build the main program, linking to the shared library. The exact build steps may vary for your compiler/platform.

like image 183
reima Avatar answered Oct 15 '22 10:10

reima