如何避免多重定义链接错误?
除了将 hello()
函数移动到另一个源 (.cpp) 文件或重命名该函数之外。还有其他方法可以避免链接错误吗?
staticLibA.h
#ifndef _STATIC_LIBA_HEADER
#define _STATIC_LIBA_HEADER
int hello(void);
int hello_staticLibA_only(void);
#endif
staticLibA.cpp
#include "staticLibA.h"
int hello(void)
{
printf("\nI'm in staticLibA\n");
return 0;
}
int hello_staticLibA_only(void)
{
printf("\nstaticLibA: hello_staticLibA_only\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);
int hello_staticLibB_only(void);
#endif
staticLibB.cpp
#include "staticLibB.h"
int hello(void)
{
printf("\nI'm in staticLibB\n");
return 0;
}
int hello_staticLibB_only(void)
{
printf("\nstaticLibB: hello_staticLibB_only\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);
extern int hello_staticLibA_only(void);
extern int hello_staticLibB_only(void);
int main(void)
{
hello();
hello_staticLibA_only();
hello_staticLibB_only();
return 0;
}
输出:
g++ -c -o main.o main.cpp
g++ -o multipleLibsTest main.o -L. -lstaticLibA -lstaticLibB -lstaticLibC -ldl -lpthread -lrt
./libstaticLibB.a(staticLibB.o): In function `hello()':
staticLibB.cpp:(.text+0x0): multiple definition of `hello()'
./libstaticLibA.a(staticLibA.o):staticLibA.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [multipleLibsTest] Error 1
Beside moving the hello()
function into another source (.cpp) file or renaming the function. Is there any other methods to avoid the linking error?
staticLibA.h
#ifndef _STATIC_LIBA_HEADER
#define _STATIC_LIBA_HEADER
int hello(void);
int hello_staticLibA_only(void);
#endif
staticLibA.cpp
#include "staticLibA.h"
int hello(void)
{
printf("\nI'm in staticLibA\n");
return 0;
}
int hello_staticLibA_only(void)
{
printf("\nstaticLibA: hello_staticLibA_only\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);
int hello_staticLibB_only(void);
#endif
staticLibB.cpp
#include "staticLibB.h"
int hello(void)
{
printf("\nI'm in staticLibB\n");
return 0;
}
int hello_staticLibB_only(void)
{
printf("\nstaticLibB: hello_staticLibB_only\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);
extern int hello_staticLibA_only(void);
extern int hello_staticLibB_only(void);
int main(void)
{
hello();
hello_staticLibA_only();
hello_staticLibB_only();
return 0;
}
output:
g++ -c -o main.o main.cpp
g++ -o multipleLibsTest main.o -L. -lstaticLibA -lstaticLibB -lstaticLibC -ldl -lpthread -lrt
./libstaticLibB.a(staticLibB.o): In function `hello()':
staticLibB.cpp:(.text+0x0): multiple definition of `hello()'
./libstaticLibA.a(staticLibA.o):staticLibA.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [multipleLibsTest] Error 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您似乎拥有这两个库,因此不清楚为什么您无法重命名该函数...
在您的
main
中,您有这一行:如果您进行链接,您期望在这里发生什么错误消失了吗?它应该调用
LibA
或LibB
中的实现吗?依靠将库传递给链接器的顺序来确定链接哪个函数似乎是一个非常糟糕的主意。在真实的示例中,如果您的hello_staticLibB_only
函数调用hello()
会发生什么?它最终可能会调用其他库中的函数版本...当您使用
g++
时,您应该考虑将库函数放入命名空间
(它们旨在帮助您避免这种命名冲突)。这将使您的代码和链接器能够区分方法之间的差异。按照
LibA
的这种方法,您将获得:staticLibA.h
staticLibA.cpp
main.cpp
Since you appear to own both libraries, it's unclear why you can't rename the function...
In your
main
, you have this line:What are you expecting to happen here, if you make the linking error go away? Should it call the implementation in
LibA
, orLibB
? Relying on the order that you pass the libraries to the linker to determine which function gets linked seems like a very bad idea. In a real example, what would happen if yourhello_staticLibB_only
function was callinghello()
? It could end up calling the version of the function that's in the other library...As you're using
g++
, you should consider putting your library functions into anamespace
(they're designed to help you avoid this kind of nameing conflict). This would allow both your code and the linker to tell the difference between the methods.Following this approach for
LibA
, you would have:staticLibA.h
staticLibA.cpp
main.cpp
链接错误特指 hello.这是因为两个库都提供了“hello”的定义。这里没有其他链接错误。
您可以将 hello 放在单独的库中,让 hello 驻留在单独的库中,或者仅具有针对 hello 对象文件的可执行链接 [hello.o]
The linking error specifically refers to hello. This shows up because both libraries provide definitions of "hello". There is no other linking error here.
You can either put hello in a separate library, have hello reside in a separate library, or just have the executable link against a hello object file [hello.o]