问题
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
#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;
}
output:
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;
}
output:
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;
}
output:
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
回答1:
Since you appear to own both libraries, it's unclear why you can't rename the function...
In your main
, you have this line:
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...
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.
Following this approach for LibA
, you would have:
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;
}
回答2:
The linking error specifically refers to hello. This shows up because both libraries provide definitions of "hello". There is no other linking error here.
You can either put hello in a separate library, have hello reside in a separate library, or just have the executable link against a hello object file [hello.o]
来源:https://stackoverflow.com/questions/4654365/how-to-avoid-multiple-definition-linking-error