一个关于gcc编绎一个简单程序的奇怪问题!?

fi9 2003-02-18 11:21:53
昨晚想到去linux下写个动态/静态库,所以就写了下面的两个程序:
// test_library.h
int test(void);

// test_library.cpp
#include <stdio.h>
#include "test_library.h"

int test(void)
{
printf("test library!\n");
return(0);
}

// test.cpp
#include "test_library.h"

int main(int nP, char *cpP)
{
test();
return(0);
}

然后
gcc -c test_library.cpp -o test_library.o
gcc -shared -Wl,soname,libtest.so.1 test_library.o -o libtest.so.1.0
或者
ar cqs test_library.o -o libtest.a
以上都成功地生成动态库和静态库

接着是编绎测试程序test
gcc -c test.cpp -o test.o //成功
gcc test.o -L/current_dir -ltest -o test //出错
出错的大意是:链接错误!找不到函数__gxx_perso???_vol()!
我用nm test.o来查看到以下内容(大概吧)
__gxx_perso???_vol() // 为什么第一个函数就是这个呢?!
// 它又是怎么出来的?!我的程序里没有这个函数啊!
main()
test()
...全文
43 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zanglan 2003-02-18
  • 打赏
  • 举报
回复
在头文件里改成
#ifdef __cplusplus
extern "C" {
#endif
int test(void);
#ifdef __cplusplus
}
#endif
sum_z 2003-02-18
  • 打赏
  • 举报
回复
把gcc全部改为g++即可

名字空间的问题。
shornmao 2003-02-18
  • 打赏
  • 举报
回复
用gcc *.c或者g++ *.cpp
satty 2003-02-18
  • 打赏
  • 举报
回复
是不是c和c++的转换问题?
你用c来编写程序
却用c++的规范来编译,这样会造成问题
你试试看都用c或都用c++
fi9 2003-02-18
  • 打赏
  • 举报
回复
TO ckacka(小红帽(什么时候变红星呢?)):
这应该不关库的问题吧!
因为这是出现在test.cpp中!
但是,如果去掉test()它就OK了!
为什么?!
// test.cpp
//#include "test_library.h"

int main(int nP, char *cpP)
{
//test();
return(0);
}
ckacka 2003-02-18
  • 打赏
  • 举报
回复
你的库的导出函数用了C++的写法

大概是这个原因吧!

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧