编译共享库的时候,有时候书写错误,比如snprintf写成了snrpintf,编译共享库的时候没有报错,但是后面使用共享库的其它程序链接的时候报错了。华为的VRP8系统,使用大量的so,并采用dlopen的方式加载,经常出现未定义符号而单板起不来的问题。
可以在编译共享库的时候指定:-Wl,--no-undefined,这样有未定义符号就报错。
1 2 3 4 5 6 | #include <stdio.h> void a() { abc(); } |
编译,不报错:
$ cc --shared -o test.so -fPIC test.c
test.c: In function ‘a’:
test.c:6:2: warning: implicit declaration of function ‘abc’ [-Wimplicit-function-declaration]
abc();
加上,-Wl,--no-undefined,就报错了:
$ cc --shared -o test.so -fPIC -Wl,--no-undefined test.c
test.c: In function ‘a’:
test.c:6:2: warning: implicit declaration of function ‘abc’ [-Wimplicit-function-declaration]
abc();
^~~
/tmp/ccxnOYSa.o: In function `a':
test.c:(.text+0xa): undefined reference to `abc'
collect2: error: ld returned 1 exit status