I have a C Project which uses a C++ library. However, if i compile the C Project i get a long list of "undefined reference to XY" errors.
Those errors are caused by C++ references:
Errors while compiling:
In function `<c function>':
implicit declaration of function `<function>'
[...]
(list shorted)
Where <c function> is the calling function from C Project and <function> the function from the C++ library.
Errors while linking:
undefined reference to `std::cerr'
undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
[...]
undefined reference to `__gxx_personality_v0'
more undefined references to `__gxx_personality_v0' follow
(lots of errors - example shorted)
In the static C++ library my types / functions are enclosed by extern "C"
block:
#ifdef __cplusplus
extern "C"
{
#endif
void example();
#ifdef __cplusplus
}
#endif
To ensure this, all the #include
's referring to C++ library are surrounded by such a extern "C"
block too.
Compiler: The C++ library is compiled using g++, the C project using gcc - both from MinGW.
CL (Linking): gcc -L<search-path here> -shared [...] -lstdc++ -l<C++ library set here>
Please note the -lstdc++
flag.
Note:
- The C project contains only
.c
and.h
files – no.cpp
- Putting a "dummy" cpp file in like stated here didn't work
- MinGW and Eclipse CDT Indigo
- The C++ Library has a name like
lib<NAME>.a
- it's linked with-l<NAME>
, search path is set.
Edit:
Using g++
as linker: undefined reference to
std::cerr'` etc. are gone, but i get another list of errors:
Warning: .drectve `-aligncomm:"___hexdig_D2A",5' unrecognized
undefined reference to `<function / class / method from C++ Library>'
[...]
undefined reference to `_Unwind_Resume'
[...]
undefined reference to `__gxx_personality_v0'
more undefined references to `__gxx_personality_v0' follow
undefined reference to `__chkstk_ms'
[...]
(list shorted)
eg. _Unwind_Resume
is available in nm
of the c++ library.
Example:
This part is in the Library (C++):
Test.h - Header
#ifndef TEST_H_
#define TEST_H_
// --- Pure C++ here ---
#ifdef __cplusplus
#include <string> /* (1) */
class Test
{
public:
Test();
void aMethod();
private:
int i;
};
#endif
// --- Mixed (C / C++ ) here ---
#ifdef __cplusplus
extern "C"
{
#endif
extern void doTest();
extern void invoke();
#ifdef __cplusplus
}
#endif
#endif /* TEST_H_ */
Test.cpp - Implementation of C++ part
#include "Test.h"
Test::Test() : i(0) { }
void Test::aMethod()
{
this->i++;
}
void invoke()
{
Test i;
i.aMethod();
}
Test.c - Implementation of C Interface (still in library!)
#include "Test.h"
void doTest()
{
invoke();
}
Now, here comes the part of the second project (pure C):
#include "Test.h"
int main(int argc, char* argv[])
{
doTest();
return 0;
}
(1)
: If i remove that include i can compile the C Project. But adding one include for any kind of class or c++ stuff causes lots of errors while linking. unfortunately i need to include classes etc..
Btw. here's the error i get for this example:
Test.cpp:(.text+0x1b): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
Test.cpp:(.text+0x64): undefined reference to `_Unwind_Resume'
Test.cpp:(.text$_ZN4TestD1Ev[Test::~Test()]+0x12): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
Test.cpp:(.eh_frame+0x6b): undefined reference to `__gxx_personality_v0'
setting -lstdc++
flag after (!) the flag for the library i get "only" these:
Test.cpp:(.text+0x64): undefined reference to `_Unwind_Resume'
Test.cpp:(.eh_frame+0x6b): undefined reference to `__gxx_personality_v0'