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)
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.
Using Eigen in CMake Projects Eigen provides native CMake support which allows the library to be easily used in CMake projects.
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.
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.
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.
/* 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 #ifdef
s which will only trigger for the C++ compiler.
/* 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).
/* main.c */
#include "foo.h"
int main() {
foo(42);
return 0;
}
Include the interface header and use it like any other C library.
$ 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With