I have a.h and a.c that gives the interface and implementation of a function
//a.h
#ifndef A_H
#define A_H
int op();
#endif
//a.c
#include "a.h"
int op(){
return 1;
}
Similarly, I have b.h and b.c that gives the interface of a function of the same name but with a different implementation
//b.h
#ifndef B_H
#define B_H
int op();
#endif
//b.c
#include "b.h"
int op(){
return 2;
}
Now, I want to link them with the main program
#include <stdio.h>
int op();
int main(){
printf( "returned = %d\n ", op());
}
For that purpose, I first compile a.c and b.c to a.o and b.o respectively.
gcc -c a.c; gcc -c b.c
Then I am trying to link them together. An usual objective here is to let main.c choose the implementation of 'op' using the implementation that appears first in the linking command. So, ideally:
gcc a.o b.o main.c
should give me an executable that returns 1 when executed, and gcc b.o a.o main.c
should give me an executable that returns 2 when executed. But I get the error message
ld: 1 duplicate symbol for architecture x86_64
to which I am actually not that surprised. My question is: How can I let the first occurred implementation be used by main.c? Thanks.
-lA -lB
or-lB -lA
– FredK