8

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
4

2 に答える 2

11

あなたは両方のライブラリを所有しているように見えるので、関数の名前を変更できない理由は不明です...

あなたのmain中に、あなたはこの行を持っています:

hello();

リンクエラーを解消した場合、ここで何が起こると予想していますか?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

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

// 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 に答える