1

I have created the .o file as follows,

*gcc -I/home/vineeshvs/Dropbox/wel/workspace/Atmel -I /home/vineeshvs/Downloads/libusb-1.0.9 -I /home/vineeshvs/Downloads/libusb-1.0.9/libusb/ usb_comm.c hex2bin.c hex_read.c crc32.c -o vs.o -lusb-1.0*

Then I used the following command to get .so file

*gcc vs.o -shared  -o libhello.so*

Then I am getting error as follows

*vs.o: In function `__i686.get_pc_thunk.bx':

(.text+0xaa6): multiple definition of `__i686.get_pc_thunk.bx'
/usr/lib/gcc/i686-linux-gnu/4.6/crtbeginS.o:crtstuff.c:

(.text.__i686.get_pc_thunk.bx[__i686.get_pc_thunk.bx]+0x0): first defined here
vs.o: In function `_fini':

(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o:(.fini+0x0): first defined here
vs.o: In function `__data_start':

(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/gcc/i686-linux-gnu/4.6/crtbeginS.o:(.data.rel+0x0): first defined here
vs.o: In function `_init':

(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/i686-linux-gnu/4.6/crtendS.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
vs.o:(.dtors+0x4): first defined here
/usr/bin/ld.bfd.real: error in vs.o(.eh_frame); no .eh_frame_hdr table will be created.

collect2: ld returned 1 exit status

*

What could be the problem? (Thanks for stopping by :))

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
vineeshvs
  • 479
  • 7
  • 32

1 Answers1

2

The problem is that you're linking the object files, not just compiling them.

Make sure that you only compile the files, don't link them! You do that using the -c option. Do not use the -l option, you don't want to link anything at this stage. So:

gcc -c -o usb_comm.o usb_comm.c
gcc -c -o hex2bin.o hex2bin.c
gcc -c -o hex_read.o hex_read.c
gcc -c -o crc32.o crc32.c

(I omitted the -I flags to save space here.)

Then finally link all those object files into a shared library, and link against usb-1.0:

gcc -shared -o libhello.so usb_comm.o hex2bin.o hex_read.o crc32.o -lusb-1.0

You should use a Makefile though for this. Or, even better, use a proper build system, like CMake, which is very easy to use. It's provided by all common Linux distros, so simply install it with the package manager (it it's not installed already), and read a quick tutorial on it.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • 1
    You might also state what the problem is up-front i.e. that he has both compiled and linked the files with the initial gcc step, which is what is producing the multiple definition error in the last step – Anya Shenanigans Jul 28 '13 at 22:51
  • @NikosC. When I tried _ gcc -fPIC -o usb_comm.o usb_comm.c _, and error comes as: ** usb_comm.c:(.text+0x742): undefined reference to hex2bin' usb_comm.c:(.text+0x74e): undefined reference to hex_read' usb_comm.c:(.text+0x7c2): undefined reference to crc32' ** When I tried _ gcc -fPIC -o hex2bin.o hex2bin.c, gcc -fPIC -o hex_read.o hex_read.c, gcc -fPIC -o crc32.o crc32.c _ error is ** (.text+0x18): undefined reference to main' collect2: ld returned 1 exit status ** – vineeshvs Jul 29 '13 at 05:35
  • @NikosC. usb_comm.c is the only main file and others (hex2bin.c, hex_read.c,crc32.c) are used in that – vineeshvs Jul 29 '13 at 05:37
  • @NikosC. yeah. It works, but only change was that I used -c in place of all -fPIC – vineeshvs Jul 29 '13 at 05:51
  • @NikosC. `-fPIC` only says to compile position independent code - this is not necessarily required for atmel (I know it's needed for x64 and sparcv9, but not always required for i386 and 32bit sparc), it's the `-c` option that compiles only, without linking – Anya Shenanigans Jul 29 '13 at 07:48
  • 1
    @vineeshvs @Petesh: Weird. I mentioned the `-c` option in the answer, but then forgot to use it in the example commands. And you're right about fPIC; I didn't actually even notice the atmel tag. – Nikos C. Jul 29 '13 at 10:48