C++调用C静态库,出现undefined reference to “” 的问题

哥们在搞一个软件C++写的,跑在linux上,中间涉及到需要调用一个内核中的静态库的步骤。
So here comes the trouble。


一开始出现undefined reference to,然而我并不方。这种问题一般都是缺少实现之类的问题。需要添加库,添加头文件路径。添加库路径。So easy!
But!
然而并没有成功,WTF,抓耳挠腮,各种路径查一遍,没错啊,
开始宕机。。。。
But again
感谢蓝灯,感谢google,感谢stackoverflow,

stackoverflow上的答案

But everything works well with other C programs linking this library.
Did you notice that C and C++ compilation create different symbol names on object file level? It’s called ‘name mangling’.
The (C++) linker would show undefined references as demangled symbols in the error message, which might confuse you. If you inspect your test.o file with nm -u you’ll see that the referenced symbol names don’t match with those provided in your library.
If you want to use functions linked in as externals that were compiled using the plain C compiler, you’ll need their function declarations enclosed in an extern “C” {} block which suppresses C++ name mangling for everything declared or defined inside, e.g.:

extern "C" 
{
    #include <dual/xalloc.h>
    #include <dual/xmalloc.h>
}
Even better, you might wrap your function declarations in your header files like this:

#if defined (__cplusplus)
extern "C" {
#endif

/*
 * Put plain C function declarations here ...
 */ 

#if defined (__cplusplus)
}
#endif

在添加头文件的时候需要告诉编译器这个是C的库,然后就完成啦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值