为什么我使用静态库编译和链接是正确的,而动态库是错误的

why I use the static library to compile and link is corrent while the dynamic library is wrong for am ace demo?

本文关键字:错误 动态 链接 静态 编译 为什么      更新时间:2023-10-16

我的gcc/g++版本是4.1.2,ACE-6.10在CentOS5.10中,我用static_libs=1选项制作了ACE库来获取静态库,在制作和制作安装后,我获得了libACE.so、libACE.a等库,然后我编写了以下代码进行测试,代码如下:

#include <ace/Log_Msg.h>
#include <ace/OS_main.h>
using namespace std;
int ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
    ACE_DEBUG( (LM_DEBUG, ACE_TEXT("Hello World!n") ) );
    return 0;
}

然后我使用以下两种方法进行编译和链接:

方法1:

 g++ -p -o acetest acetest.cpp  /usr/local/src/ACE_wrappers/lib/libACE.a -I$ACE_ROOT -I$ACE_ROOT/ace -pthread  -ldl -lrt

方法2:

[root@localhost testCode]# g++ -p -o acetest acetest.cpp  -L/usr/local/src/ACE_wrappers/lib -lACE -I$ACE_ROOT -I$ACE_ROOT/ace -pthread  -ldl -lrt
/tmp/cc0eKwlC.o: In function `main':
acetest.cpp:(.text+0x15): undefined reference to `ACE_Log_Msg::last_error_adapter()'
acetest.cpp:(.text+0x1d): undefined reference to `ACE_Log_Msg::instance()'
acetest.cpp:(.text+0x3f): undefined reference to `ACE_Log_Msg::conditional_set(char const*, int, int, int)'
acetest.cpp:(.text+0x57): undefined reference to `ACE_Log_Msg::log(ACE_Log_Priority, char const*, ...)'
collect2: ld 返回 1

问题来了,使用静态库的方法1是正确的,为什么使用动态库的方法2是错误的?

渴望答案,感谢所有人;

编译应用程序时,应向编译器添加-DACE_AS_STATIC_LIBS标志,以指示您希望与ACE 静态链接

尝试将no_hidden_visibility=1添加到platform_macros.GNU文件中。我相信ACE在构建共享库时默认会隐藏符号。

请参阅此处了解它可以提供的好处。然而,当混合静态库和动态库时,它似乎不能很好地工作。如果有人有更多关于原因的信息,请随时插话。