8

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

静态库A.h

#ifndef _STATIC_LIBA_HEADER
#define _STATIC_LIBA_HEADER

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

#endif

静态LibA.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

静态库B.h

#ifndef _STATIC_LIBB_HEADER
#define _STATIC_LIBB_HEADER

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

#endif

静态库B.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

主文件

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
4

2 回答 2

11

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

在你的main,你有这一行:

hello();

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

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

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

静态库A.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

静态LibA.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;
    }
}

主文件

// 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;
}
于 2011-06-07T11:53:44.780 回答
1

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

您可以将 hello 放在单独的库中,让 hello 驻留在单独的库中,或者只将可执行链接与 hello 目标文件 [hello.o]

于 2011-02-04T02:15:25.180 回答