在C项目中使用C ++库会导致错误的一个长长的清单 [英] Using a C++ Library in a C Project causes a long list of errors
问题描述
我有一个 C ++项目它采用了 C ++库。但是,如果我编译C项目中,我得到的一个长长的清单未定义参考XY的错误。
这些错误是由C ++引用造成的:
错误而编译:
在功能`< c函数>':
功能`&LT隐式声明;功能>'
[...]
的(列表短路)的结果
其中, c为C函数> 的是从C项目调用函数和的<作用方式> 的从C ++库中的函数
的错误而进行链接:
未定义的引用'的std :: CERR
未定义的引用'的std :: basic_ostream<焦炭,的std :: char_traits<焦炭> >&安培;的std ::运营商的LT;< <的std :: char_traits<焦炭> >(的std :: basic_ostream<焦炭,的std :: char_traits<焦炭>>&放大器;,字符常量*)'
未定义的引用'的std :: basic_ostream<焦炭,的std :: char_traits<焦炭> >&安培;的std ::运营商的LT;< <焦炭,的std :: char_traits<焦炭>中的std ::分配器<&烧焦GT; >(STD :: basic_ostream<焦炭,的std :: char_traits<焦炭>>&放;,的std :: basic_string的<焦炭,的std :: char_traits<焦炭>中的std ::分配器<焦炭>>常量和放大器;)
[...]
未定义的引用`__gxx_personality_v0
为`__gxx_personality_v0遵循多个未定义的引用
的(大量的错误 - 例如短路)的
在静态C ++库我的类型/功能是由的externC
块封闭:
的#ifdef __cplusplus
为externC
{
#万一 void示例();#IFDEF __cplusplus
}
#万一
要确保这一点,所有的的#include
的引用C ++库是由这样的的externC$ C $包围C>太块
编译器/ STRONG> - 无论是从的MinGW
CL(链接): gcc的-L<搜索路径这里> -shared [...] -lstdc ++ -l< C ++库这里集>
的请注意: -lstdc ++
标记。的
注意:
- 的C项目仅包含
.C
和.H
文件 - 没有的.cpp
- 把一个虚拟的CPP像陈述文件<一个href=\"http://stackoverflow.com/questions/376966/using-c-c-static-libraries-from-iphone-objectivec-apps\">here没有工作
- MinGW和Eclipse CDT的靛蓝
- C ++库具有类似名称
LIB&LT;名称&gt; .A
- 它与-l&LT链接;名称&gt;
,搜索路径设置。
修改
使用 G ++
作为连接器:未定义的引用
的std :: cerr'`等等都走了,但我得到错误的另一个列表:
警告:.drectve`-aligncomm:___ hexdig_D2A,5'无法识别
未定义的引用`&lt;作用/班/从C ++库的方法&gt;'
[...]
未定义的引用`_Unwind_Resume
[...]
未定义的引用`__gxx_personality_v0
为`__gxx_personality_v0遵循多个未定义的引用
未定义的引用`__chkstk_ms
[...]
的(列表短路)的
如。 _Unwind_Resume
可在纳米
的C ++的库。
例子:
这部分是在库(C ++):
Test.h - 页眉
的#ifndef TEST_H_
#定义TEST_H_// ---纯C ++在这里---
#IFDEF __cplusplus#包括LT&;串GT; / *(1)* /
类测试
{
上市:
测试();
无效amethod方法();私人的:
INT I;
};
#万一// ---混合(C / C ++)在这里---#IFDEF __cplusplus
为externC
{
#万一 EXTERN无效doTest();
EXTERN无效的invoke();#IFDEF __cplusplus
}
#万一#ENDIF / * * TEST_H_ /
Test.cpp的 - 的C ++实现的部分。
的#includeTest.h
测试::测试():I(0){}无效的Test :: amethod方法()
{
这 - I标记++;
}
无效的invoke()
{
我测试;
i.aMethod();
}
TEST.C - C接口(!仍在库)的实施
的#includeTest.h无效doTest()
{
调用();
}
现在,来了第二个项目的一部分(纯C):
的#includeTest.h
INT主(INT ARGC,CHAR *的argv [])
{
doTest(); 返回0;
}
(1)
:如果我删除,包括我可以编译C项目。但是增加一个包括任何种类的类或C ++的东西引起了大量的错误,同时连接。不幸的是我需要包括类等。
顺便说一句。这里的错误,我得到了这个例子:
Test.cpp的:(文字+ 0x1b):未定义引用'的std :: basic_string的&LT;焦炭,的std :: char_traits&LT;焦炭&gt;中的std ::分配器&LT;焦炭&GT; &GT; :: basic_string的()
。TEST.CPP :(文字+ 0x64):未定义的参考`_Unwind_Resume
TEST.CPP :(文字$ _ZN4TestD1Ev [测试::〜测试()] + 0×12):未定义引用'的std :: basic_string的&LT;焦炭,的std :: char_traits&LT;焦炭&gt;中的std ::分配器&LT;焦炭&GT; &GT; :: basic_string的〜()
。TEST.CPP :( eh_frame + 0x6b):未定义的参考`__gxx_personality_v0
设置 -lstdc ++
后旗标志库我得到只有这些(!):
Test.cpp的文字:( + 0x64):未定义的参考`_Unwind_Resume
。TEST.CPP :( eh_frame + 0x6b):未定义的参考`__gxx_personality_v0
这个问题看起来像MinGW的海湾合作委员会的错误。
- 使用 GCC 3.4.5 :错误的作为上述发布
- 使用 GCC 4.7.2 :无错误的
另外,还要确保 -lstdc ++
是过去(!)你的 -l </ code >标记。
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'
This problem looks like a bug in MinGW GCC.
- Using GCC 3.4.5: Error as posted above
- Using GCC 4.7.2: No Error
Also make sure -lstdc++
is the last (!) of your -l
flags.
这篇关于在C项目中使用C ++库会导致错误的一个长长的清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!