Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在编译我的 .so 库代码并遇到了一个奇怪的问题。在一个头文件中,我已经清除了一个变量 extern,该变量包含在其他 .c 文件中,并且没有在任何地方定义。代码编译得很好,在运行时它会出错。为什么编译时没有错误或警告?我正在使用 gcc 9。
在命令行上使用-Wl,--no-undefined 并指定函数来自的库。例如,如果 libraryx使用 library 中定义的函数y:
-Wl,--no-undefined
x
y
gcc -shared -o libx.so x.c # OK gcc -shared -o libx.so x.c -Wl,--no-undefined # undefined symbols from lib y gcc -shared -o libx.so x.c -ly -Wl,--no-undefined # OK again