ILD

编译共享库的时候强制检查未定义符号
作者:Yuan Jianpeng 邮箱:yuanjp89@163.com
发布时间:2019-8-20 站点:Inside Linux Development

编译共享库的时候,有时候书写错误,比如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


https://stackoverflow.com/questions/2356168/force-gcc-to-notify-about-undefined-references-in-shared-libraries


Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.