为什么我没有得到“多重定义” g++ 的错误?
我尝试使用 g++ 将我的可执行程序与 2 个静态库链接。 2个静态库具有相同的函数名称。我期待链接器发出“多重定义”链接错误,但我没有收到。任何人都可以帮忙解释为什么会这样吗?
staticLibA.h
#ifndef _STATIC_LIBA_HEADER
#define _STATIC_LIBA_HEADER
int hello(void);
#endif
staticLibA.cpp
#include "staticLibA.h"
int hello(void)
{
printf("\nI'm in staticLibA\n");
return 0;
}
输出:
g++ -c -Wall -fPIC -m32 -o staticLibA.o staticLibA.cpp
ar -cvq ../libstaticLibA.a staticLibA.o
a - staticLibA.o
staticLibB.h
#ifndef _STATIC_LIBB_HEADER
#define _STATIC_LIBB_HEADER
int hello(void);
#endif
staticLibB.cpp
#include "staticLibB.h"
int hello(void)
{
printf("\nI'm in staticLibB\n");
return 0;
}
输出:
g++ -c -Wall -fPIC -m32 -o staticLibB.o staticLibB.cpp
ar -cvq ../libstaticLibB.a staticLibB.o
a - staticLibB.o
main.cpp
extern int hello(void);
int main(void)
{
hello();
return 0;
}
输出:
g++ -c -o main.o main.cpp
g++ -o multipleLibsTest main.o -L. -lstaticLibA -lstaticLibB -lstaticLibC -ldl -lpthread -lrt
I tried to link my executable program with 2 static libraries using g++. The 2 static libraries have the same function name. I'm expecting a "multiple definition" linking error from the linker, but I did not received. Can anyone help to explain why is this so?
staticLibA.h
#ifndef _STATIC_LIBA_HEADER
#define _STATIC_LIBA_HEADER
int hello(void);
#endif
staticLibA.cpp
#include "staticLibA.h"
int hello(void)
{
printf("\nI'm in staticLibA\n");
return 0;
}
output:
g++ -c -Wall -fPIC -m32 -o staticLibA.o staticLibA.cpp
ar -cvq ../libstaticLibA.a staticLibA.o
a - staticLibA.o
staticLibB.h
#ifndef _STATIC_LIBB_HEADER
#define _STATIC_LIBB_HEADER
int hello(void);
#endif
staticLibB.cpp
#include "staticLibB.h"
int hello(void)
{
printf("\nI'm in staticLibB\n");
return 0;
}
output:
g++ -c -Wall -fPIC -m32 -o staticLibB.o staticLibB.cpp
ar -cvq ../libstaticLibB.a staticLibB.o
a - staticLibB.o
main.cpp
extern int hello(void);
int main(void)
{
hello();
return 0;
}
output:
g++ -c -o main.o main.cpp
g++ -o multipleLibsTest main.o -L. -lstaticLibA -lstaticLibB -lstaticLibC -ldl -lpthread -lrt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
链接器不会查看
staticLibB
,因为在链接staticLibA
时,不存在未实现的依赖项。The linker does not look at
staticLibB
, because by the timestaticLibA
is linked, there are no unfulfilled dependencies.这是一件容易的事。仅当引用的符号尚未定义时,才会从库中提取对象。仅提取其中一个问候语(来自 A)。如果链接 .o 文件,则会出现错误。
That's an easy one. An object is only pulled out of a library if the symbol referenced hasn't already been defined. Only one of the hellos are pulled (from A). You'd get errors if you linked with the .o files.
当链接器尝试将
main.o
链接到multipleLibsTest
并发现hello()
未解析时,它开始按照给定的顺序搜索库在命令行上。它将在staticLibA
中找到hello()
的定义并终止搜索。它根本不会在
staticLibB
或staticLibC
中查找。如果
staticLibB.o
包含staticLibA
中没有的另一个符号,并且该符号被拉入最终的可执行文件中,那么您会收到hello
错误的多重定义,当单个.o
文件从库中取出时,其中两个文件将具有hello()
。在链接命令行上颠倒staticLibA
和staticLibB
的顺序将使该错误消失。When the linker tries to link
main.o
intomultipleLibsTest
and sees thathello()
is unresolved, it starts searching the libraries in the order given on the command line. It will find the definition ofhello()
instaticLibA
and will terminate the search.It will not look in
staticLibB
orstaticLibC
at all.If
staticLibB.o
contained another symbol not instaticLibA
and that was pulled into the final executable, you then get a multiple definition ofhello
error, as individual.o
files are pulled out of the library and two of them would havehello()
. Reversing the order ofstaticLibA
andstaticLibB
on the link command line would then make that error go away.