c语言中未定义库的错误,在C项目中使用C库会导致很长的错误列表

我有一个使用C库的C项目.但是,如果我编译C项目,我会得到一个很长的“未定义的XY参考”错误列表.

这些错误是由C引用引起的:

编译时出错:

In function `':

implicit declaration of function `'

[...]

(名单缩短)

其中< c function>是来自C Project和< function>的调用函数.来自C库的功能.

链接时出错:

undefined reference to `std::cerr'

undefined reference to `std::basic_ostream >& std::operator<< <:char_traits> >(std::basic_ostream >&, char const*)'

undefined reference to `std::basic_ostream >& std::operator<< , std::allocator >(std::basic_ostream >&, std::basic_string, std::allocator > const&)'

[...]

undefined reference to `__gxx_personality_v0'

more undefined references to `__gxx_personality_v0' follow

(很多错误 – 示例短路)

在静态C库中,我的类型/函数由extern“C”块包围:

#ifdef __cplusplus

extern "C"

{

#endif

void example();

#ifdef __cplusplus

}

#endif

为了确保这一点,所有引用C库的#include也被这样的外部“C”块包围.

编译器:使用g编译C库,使用gcc编译C项目 – 均来自MinGW.

CL(链接):gcc -L< search-path here> -shared […] -lstdc -l< C库设置在这里>

请注意-lstdc标志.

注意:

> C项目仅包含.c和.h文件 – 没有.cpp

>将类似于here的“虚拟”cpp文件放入不起作用

> MinGW和Eclipse CDT Indigo

> C库的名称类似于lib< NAME> .a – 它与-l< NAME>链接,搜索路径已设置.

编辑:

使用g作为链接器:未定义的引用tostd :: cerr’`等已经消失,但我得到另一个错误列表:

Warning: .drectve `-aligncomm:"___hexdig_D2A",5' unrecognized

undefined reference to `'

[...]

undefined reference to `_Unwind_Resume'

[...]

undefined reference to `__gxx_personality_v0'

more undefined references to `__gxx_personality_v0' follow

undefined reference to `__chkstk_ms'

[...]

(名单缩短)

例如. _Unwind_Resume在c库的nm中可用.

例:

这部分在图书馆(C)中:

Test.h – 标题

#ifndef TEST_H_

#define TEST_H_

// --- Pure C++ here ---

#ifdef __cplusplus

#include /* (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 – C部分的实现

#include "Test.h"

Test::Test() : i(0) { }

void Test::aMethod()

{

this->i++;

}

void invoke()

{

Test i;

i.aMethod();

}

Test.c – C接口的实现(仍然在库中!)

#include "Test.h"

void doTest()

{

invoke();

}

现在,第二个项目的部分(纯C):

#include "Test.h"

int main(int argc, char* argv[])

{

doTest();

return 0;

}

(1):如果我删除包含我可以编译C项目.但是添加一个包含任何类或类的东西会在链接时导致很多错误.不幸的是,我需要包括类等.

顺便说一句.这是我为这个例子得到的错误:

Test.cpp:(.text+0x1b): undefined reference to `std::basic_string, std::allocator >::basic_string()'

Test.cpp:(.text+0x64): undefined reference to `_Unwind_Resume'

Test.cpp:(.text$_ZN4TestD1Ev[Test::~Test()]+0x12): undefined reference to `std::basic_string, std::allocator >::~basic_string()'

Test.cpp:(.eh_frame+0x6b): undefined reference to `__gxx_personality_v0'

在(!)库的标志之后设置-lstdc标志我得到“仅”这些:

Test.cpp:(.text+0x64): undefined reference to `_Unwind_Resume'

Test.cpp:(.eh_frame+0x6b): undefined reference to `__gxx_personality_v0'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值