使用动态链接库出现 undefined reference to问题

标签:
佛学 |
分类: linux内核 |
-
(.text+0x13):
undefined reference to `func'
1.
http://img1.51cto.com/attachment/201011/133144914.pngundefined
-
gcc
-c test.c -
gcc
–c main.c
-
main.o:
In function `main': -
main.c:(.text+0x7):
undefined reference to `test' -
collect2:
ld returned 1 exit status
-
gcc
-o main main.o test.o
-
gcc
-o //缺少test()的实现文件main main.c
需要改成如下形式才能成功,将test()函数的实现文件一起编译。
-
gcc
-o //ok,没问题了main main.c test.c
2.
http://img1.51cto.com/attachment/201011/133734483.pngundefined
-
gcc
-c test.c -
ar
-rc test.a test.o
-
gcc
-c main.c
-
gcc
-o main main.o
-
/tmp/ccCPA13l.o:
In function `main': -
main.c:(.text+0x7):
undefined reference to `test' -
collect2:
ld returned 1 exit status
-
gcc
-o //注:./main main.o ./test.a 是给出了test.a的路径
-
gcc
-o //同样,如果不加test.a也会报错main main.c ./test.a
3.
http://img1.51cto.com/attachment/201011/134157339.pngundefined
-
gcc
-c func.c - gcc
-c test.c -
gcc
-c main.c
-
ar
–rc func.a func.o -
ar
–rc test.a test.o
-
gcc
-o main main.o test.a
-
test.a(test.o):
In function `test': -
test.c:(.text+0x13):
undefined reference to `func' -
collect2:
ld returned 1 exit status
-
gcc
-o main main.o test.a func.a
4 多个库文件链接顺序问题
-
gcc
-o main main.o func.a test.a
-
test.a(test.o):
In function `test': -
test.c:(.text+0x13):
undefined reference to `func' -
collect2:
ld returned 1 exit status
5. 在c++代码中链接c语言的库
-
gcc
-c test.c -
ar
-rc test.a test.o
-
g++
-o main main.cpp test.a
-
/tmp/ccJjiCoS.o:
In function `main': -
main.cpp:(.text+0x7):
undefined reference to `test()' -
collect2:
ld returned 1 exit status
-
g++
-o main main.cpp test.a