Lake_Lagunita
Lake_Lagunita

Reputation: 553

a contradictory result when checking an example of "Linking with external libraries"

I just started to learn gcc/g++ by reading "An introduction to GCC"

In chapter 3.1 the author showed an example of linking with external libraries and header files. The main source code uses 'gdbm.h' header (installed at /opt/gdbm-1.8.3/include/ in text book) file and the library ‘libgdbm.a’ (installed at /opt/gdbm-1.8.3/lib/ in text book). The process of linking with the library and header file is as follows:

Link to example screenshot

When I tried to repeat this, I got a contradictory result. When I used method 1 (in the example fails) which only includes header file path, the program can be compiled, however, when I used method 2 (in the example succeeds), I got an error, saying that "ld: 1 duplicate symbol for architecture x86_64 clang: error: linker command failed with exit code 1"

(this is my full command line and error message:)

userMacBook-Pro:03 user$gcc -Wall -I/usr/local/include dbmain.c -L/usr/local/lib dbmain.c -lgdbm
duplicate symbol _main in:
/var/folders/6_/09vfzzms7dq1d73vl4mwlxmh0000gn/T/dbmain-9d15f9.o
/var/folders/6_/09vfzzms7dq1d73vl4mwlxmh0000gn/T/dbmain-907e96.o

this is my command that succeeds:

userMacBook-Pro:03 user$ gcc -Wall -I/usr/local/include dbmain.c -lgdbm
userMacBook-Pro:03 user$ ./a.out
Storing key-value pair... done.

ld: 1 duplicate symbol for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Can anyone explain why this happens?

I don't know which platform the author uses, I just use mac OS X.

Upvotes: 0

Views: 64

Answers (1)

dlask
dlask

Reputation: 8982

gcc -Wall -I/usr/local/include dbmain.c -L/usr/local/lib dbmain.c -lgdbm

The file dbmain.c is present twice on your command line. For this reason the main function is present twice.

Upvotes: 1

Related Questions