我试图将静态库(使用gcc编译)链接到C++程序,并得到'未定义引用'。我在Ubuntu 12.04服务器机器上使用了gcc和g ++版本4.6.3。例如,这里是因法简单的库文件:将C编译静态库链接到C++程序
mylib.h
#ifndef __MYLIB_H_
#define __MYLIB_H_
int factorial(int n);
#endif
mylib.c
#include "mylib.h"
int factorial(int n)
{
return ((n>=1)?(n*factorial(n-1)):1);
}
我使用创建的对象,这mylib.c gcc:
gcc -o mylib.o -c mylib.c
再次静态库是从对象文件中使用AR实用工具创建:
ar -cvq libfact.a mylib.o
我测试该文库用C程序(test.c的)和C++程序(TEST.CPP)
C和C++程序具有相同体:
#include "mylib.h"
int main()
{
int fact = factorial(5);
return 0;
}
假设静态库libfact.a是在/ home/test目录可用,我编译的C程序没有任何问题:
gcc test.c -L/home/test -lfact
但是在测试时的C++程序,它投掷链接错误:
g++ test.cpp -L/home/test -lfact
test.cpp:(.text+0x2f): undefined reference to `factorial(int)'
collect2: ld returned 1 exit status
我甚至试图在TEST.CPP添加的extern命令:
extern int factorial(int n) //added just before the main() function
还是一样的错误。
- 有人能告诉我我在这里错了吗?
- 创建静态库时是否有任何我错过的东西?
- 我必须在我的
test.cpp
中添加任何东西才能使其工作?
当我看到SO问题没有upvote,还有一个7-upvoted的答案时,我完全理解愚蠢的人类是多么的愚蠢。如果问题不是这样的话,答案如何相关?来吧! –