如何避免多重定义链接错误? [英] How to avoid multiple definition linking error?

查看:319
本文介绍了如何避免多重定义链接错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 / p>

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 / p>

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


推荐答案

在你的中,你不知道为什么你不能重命名这个函数。 $ c>,你有这行:

In your main, you have this line:

hello();

如果你让链接错误消失,你期望在这里发生什么?是否应该调用 LibA LibB 中的实现?依赖于将库传递给链接器以确定哪个函数链接的顺序看起来像是一个很糟糕的主意。在一个实际的例子中,如果你的 hello_staticLibB_only 函数调用 hello()会发生什么?它可以最终调用其他库中的函数的版本...

What are you expecting to happen here, if you make the linking error go away? Should it call the implementation in LibA, or LibB? 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 your hello_staticLibB_only function was calling hello()? It could end up calling the version of the function that's in the other library...

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

As you're using g++, you should consider putting your library functions into a namespace (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.

按照 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

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;
    }
}

main.cpp / p>

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;
}

这篇关于如何避免多重定义链接错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆