Comment éviter la définition de plusieurs erreur de couplage?

À côté de déplacement de la hello() fonction dans une autre source (.rpc) de fichiers ou renommer la fonction. Est-il d'autres méthodes pour éviter l'erreur de couplage?

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

de sortie:

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

de sortie:

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

de sortie:

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

OriginalL'auteur ban | 2011-01-11