强制GCC通知有关共享库中未定义的引用 [英] Force GCC to notify about undefined references in shared libraries
问题描述
我有一个与另一个(第三方)共享库链接的共享库。我的共享库然后使用dlopen在我的应用程序中加载。所有这些工作正常(假设文件在正确的路径等)。
现在,问题是我甚至不需要指定链接到第三方,当我链接我的图书馆的时候。 GCC接受它,而不报告有关未定义引用的错误。所以,问题; 如何强制GCC通知我有关未定义的引用
如果我将我的库更改为(临时)可执行文件,引用(当不向链接器提供库时)。
即可完成以下操作:
g ++ -fPIC -shared -o libb.so bo
g ++ -fPIC -shared -o liba.so ao
g ++ -o a.exe a.cpp
其中第二行不提供错误,第三行引用了未定义的引用。
示例代码:
ah:
class a
{
public:
void foobar();
};
a.cpp:
#includeah
#includebh
void a :: foobar()
{
b myB;
myB.foobar();
}
int main()
{
a myA; myA.foobar();
}
bh:
class b
{
public:
void foobar();
};
b.cpp:
#includebh
构建共享库时可以使用pre>
void b :: foobar()
{
}
解决方案-Wl, - no-undefined ,未定义的符号将显示为链接器错误。
g ++ -shared -Wl,-soname,libmylib.so.5 -Wl, -no-undefined -o libmylib.so.1.1 mylib.o -lthirdpartylib
I have a shared library that is linked with another (third-party) shared library. My shared library is then loaded using dlopen in my application. All this works fine (assuming files are in the proper path etc).
Now, the problem is that I don't even need to specify to link against the third-party shared library when I link my library. GCC accept it without reporting errors about undefined references. So, the question; how can I force GCC to notify me about undefined references?
If I change my library to be (temporarily) an executable, I get undefined references (when not supplying the library to the linker). (Works fine if I specify it.)
I.e., the following is done:
g++ -fPIC -shared -o libb.so b.o g++ -fPIC -shared -o liba.so a.o g++ -o a.exe a.cpp
Where the second line does NOT give out an error and the third line complains about an undefined reference.
Sample code:
a.h:
class a { public: void foobar(); };
a.cpp:
#include "a.h" #include "b.h" void a::foobar() { b myB; myB.foobar(); } int main() { a myA; myA.foobar(); }
b.h:
class b { public: void foobar(); };
b.cpp:
#include "b.h" void b::foobar() { }
解决方案-Wl,--no-undefined linker option can be used when building shared library, undefined symbols will be shown as linker errors.
g++ -shared -Wl,-soname,libmylib.so.5 -Wl,--no-undefined -o libmylib.so.1.1 mylib.o -lthirdpartylib
这篇关于强制GCC通知有关共享库中未定义的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!