Why I'm not getting "Multiple definition" error from the g++?

Posted by ban on Stack Overflow See other posts from Stack Overflow or by ban
Published on 2011-01-10T02:19:30Z Indexed on 2011/01/10 8:53 UTC
Read the original article Hit count: 177

Filed under:
|
|
|
|

I tried to link my executable program with 2 static libraries using g++. The 2 static libraries have the same function name. I'm expecting a "multiple definition" linking error from the linker, but I did not received. Can anyone help to explain why is this so?

staticLibA.h

#ifndef _STATIC_LIBA_HEADER
#define _STATIC_LIBA_HEADER

int hello(void);

#endif

staticLibA.cpp

#include "staticLibA.h"

int hello(void)
{
    printf("\nI'm in staticLibA\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);

#endif

staticLibB.cpp

#include "staticLibB.h"

int hello(void)
{
    printf("\nI'm in staticLibB\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);

int main(void)
{
 hello();
 return 0;
}

output:

g++ -c  -o main.o main.cpp
g++ -o multipleLibsTest main.o  -L. -lstaticLibA -lstaticLibB -lstaticLibC -ldl -lpthread -lrt

© Stack Overflow or respective owner

Related posts about linux

Related posts about linker