Home > Software engineering >  Cannot compile hackrf.c
Cannot compile hackrf.c

Time:03-16

I try to compile/build hackrf.c since i'm using the library in another code, I have hackrf.h in the same directory as the one i'm building hackrf.c in...

https://github.com/greatscottgadgets/hackrf/tree/master/host/libhackrf/src

Ideally, it should do so, but instead I get these errors in the terminal:

choza@chozaUbuntu:~/Documents/workspace_c$ gcc hackrf.c
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text 0x24): undefined reference to `main'
/usr/bin/ld: /tmp/ccrRkrH1.o: in function `cancel_transfers':
hackrf.c:(.text 0x139): undefined reference to `libusb_cancel_transfer'
/usr/bin/ld: /tmp/ccrRkrH1.o: in function `free_transfers':
hackrf.c:(.text 0x25b): undefined reference to `libusb_free_transfer'
/usr/bin/ld: /tmp/ccrRkrH1.o: in function `allocate_transfers':
hackrf.c:(.text 0x337): undefined reference to `libusb_alloc_transfer'
/usr/bin/ld: /tmp/ccrRkrH1.o: in function `prepare_transfers':
hackrf.c:(.text 0x484): undefined reference to `libusb_submit_transfer'
/usr/bin/ld: /tmp/ccrRkrH1.o: in function `detach_kernel_drivers':
hackrf.c:(.text 0x4ee): undefined reference to `libusb_get_device'
/usr/bin/ld: hackrf.c:(.text 0x505): undefined reference to `libusb_get_active_config_descriptor'
/usr/bin/ld: hackrf.c:(.text 0x53b): undefined reference to `libusb_free_config_descriptor'
/usr/bin/ld: hackrf.c:(.text 0x555): undefined reference to `libusb_kernel_driver_active'
/usr/bin/ld: hackrf.c:(.text 0x592): undefined reference to `libusb_detach_kernel_driver'

And so on...

Then I suppose I should directly link libusb... But I get this:

choza@chozaUbuntu:~/Documents/workspace_c$ gcc hackrf.c -lusb-1.0
/usr/bin/ld: /tmp/ccfIzbqD.o: undefined reference to symbol 'pthread_join@@GLIBC_2.2.5'
/usr/bin/ld: /lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line

And pthread...

choza@chozaUbuntu:~/Documents/workspace_c$ gcc hackrf.c -lusb-1.0 -pthread
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text 0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status

I really don't know how to fix the error above or how to successfully compile the .c file anymore.

I'm using VS Code in Ubuntu 20.04.

What should I do?

CodePudding user response:

I really don't know how to fix the error above or how to successfully compile the .c file anymore.

You aren't having a problem compiling, you are having a problem linking.

The reason your link fails is that hackrf.c lacks main() function, which every C program requires. And that's because it's part of a library.

Generally you'll want to build the entire project, not just a library, using the instructions the project provides.

If you do want to build a library, then you should link it with the rest of your program.

  • Related