如何避免多重定义链接错误?

ban*_*ban 8 c++ linux linker g++ multiple-definition-error

除了将hello()函数移动到另一个源(.cpp)文件或重命名该函数.有没有其他方法可以避免链接错误?

staticLibA.h

#ifndef _STATIC_LIBA_HEADER
#define _STATIC_LIBA_HEADER

int hello(void);
int hello_staticLibA_only(void);

#endif
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

输出:

g++ -c -Wall -fPIC -m32 -o staticLibA.o staticLibA.cpp
ar -cvq ../libstaticLibA.a staticLibA.o
a - staticLibA.o
Run Code Online (Sandbox Code Playgroud)

staticLibB.h

#ifndef _STATIC_LIBB_HEADER
#define _STATIC_LIBB_HEADER

int hello(void);
int hello_staticLibB_only(void);

#endif
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

输出:

g++ -c -Wall -fPIC -m32 -o staticLibB.o staticLibB.cpp 
ar -cvq ../libstaticLibB.a staticLibB.o 
a - staticLibB.o
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)

for*_*rir 11

由于您似乎拥有两个库,因此不清楚为什么您无法重命名该函数...

在你的main,你有这条线:

hello();
Run Code Online (Sandbox Code Playgroud)

如果你使链接错误消失,你期望在这里发生什么?它应该调用实现LibA,还是LibB?依赖于将库传递给链接器以确定链接哪个函数的顺序似乎是一个非常糟糕的主意.在一个真实的例子中,如果你的hello_staticLibB_only函数正在调用会发生什么hello()?它可能最终调用其他库中的函数版本...

在您使用时g++,您应该考虑将库函数放入namespace(它们旨在帮助您避免这种名称冲突).这将允许您的代码和链接器告诉方法之间的区别.

按照这种方法LibA,您将拥有:

staticLibA.h

#ifndef _STATIC_LIBA_HEADER
#define _STATIC_LIBA_HEADER

// Declare namespace to keep library functions together
namespace LibA {
    int hello(void);
    int hello_staticLibA_only(void);
}

#endif
Run Code Online (Sandbox Code Playgroud)

staticLibA.cpp

#include "staticLibA.h"
#include <stdio.h>

// Indicate that contained function definitions belong in the LibA namespace
namespace LibA {
    int hello(void)
    {
        printf("\nI'm in staticLibA\n");
        return 0;
    }

    int hello_staticLibA_only(void)
    {
        printf("\nstaticLibA: hello_staticLibA_only\n");
        return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

main.cpp中

// These declarations would usually be in a header... but I've left
// them here to match your sample code...

// declare relevant functions to belong to the LibA namespace
namespace LibA{
    extern int hello(void);
    extern int hello_staticLibA_only(void);
}

// declare relevant functions from LibB (note they are not
// in a namespace)
extern int hello(void);
extern int hello_staticLibB_only(void);

int main(void)
{
    // Explicitly call the hello from LibA
    LibA::hello();
    // Call the other library function (also from LibA)
    LibA::hello_staticLibA_only();

    // Call library functions from LibB (note they don't require a namespace
    // because I haven't updated it to have one)
    hello();
    hello_staticLibB_only();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)


Foo*_*Bah 1

链接错误特指 hello. 这是因为两个库都提供了“hello”的定义。这里没有其他链接错误。

您可以将 hello 放在单独的库中,让 hello 驻留在单独的库中,或者仅具有针对 hello 对象文件的可执行链接 [hello.o]