Undefined reference to `libusb_handle_events_completed` - c

I'm trying to compile mphidflash on Debian (Jessie) after re-writing the low-level USB interface to talk to libhidapi-libusb instead of libhid (which is no longer supported on Debian) but I'm getting some compiler errors that I'm not sure what do to with.
These are my includes in usb-linux.c:
#include <stdio.h>
#include <usb.h>
#include <hidapi/hidapi.h>
#include "mphidflash.h"
#include <errno.h>
and here are the commands I've tried which error:
$ gcc main.o hex.o usb-linux.o -lusb -lhidapi-libusb -o mphidflash
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/libhidapi-libusb.so: undefined reference to `libusb_handle_events_completed'
collect2: error: ld returned 1 exit status
$ gcc main.o hex.o usb-linux.o /usr/lib/x86_64-linux-gnu/libhidapi-libusb.a -lusb -lhidapi-libusb -o mphidflash
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libhidapi-libusb.a(hid.o): undefined reference to symbol 'libusb_detach_kernel_driver'
//usr/local/lib/libusb-1.0.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Basically, I'm getting an undefined reference to symbol 'libusb_detach_kernel_driver' which suggests that I'm including the libusb library incorrectly but I've no idea what else to try.

Turns out hidapi-libusb uses libusb-1.0 and not libusb-0.1 (both of which can be installed on Debian).
I changed the includes to be:
#include <stdio.h>
#include <libusb-1.0/libusb.h>
#include <hidapi/hidapi.h>
#include "mphidflash.h"
#include <errno.h>
And compiled with:
$ gcc main.o hex.o usb-linux.o -lusb-1.0 -lhidapi-libusb -o mphidflash
I'll be putting my changes into a git repo and I'll post the link when I'm finished.

Related

Why do I keep getting a linker error even after compiling my header file?

I keep getting this error when compiling!
Undefined symbols for architecture x86_64:
"_lex", referenced from:
_main in main-7a45f7.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I am compiling a file main.c. Here are the first few lines of the program:
#include <stdio.h>
#include "lex.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
I thought compiling the header file "lex.h" might have been the issue, but I was apparently wrong. I ran the command
gcc -c lex.c lex.o
and received the following warning (but no errors):
clang: warning: lex.o: 'linker' input unused [-Wunused-command-line-argument]
I think I have included all the information needed, but these are the files in my current directory (as per ls):
lex.c lex.h lex.o main.c
Any help would be appreciated, thanks!
Thanks to Tom Karzes and Itagaki Fumihiko for help. The fatal flaw in my compilation was not linking main.o and lex.o. To compile the files properly, this is what you should actually do:
gcc -c lex.c
gcc -c main.c
gcc main.o lex.o

Do I need to compile separately included header files from another direcotry?

I'm working on a project (hangman.c) and I want to include a module I wrote from another directory (quicksort.h). Here is the top of my hangman.c file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../quicksort/quicksort.h"
I've been running this command:
clang -o hangman hangman.c
And i get this error message:
/usr/bin/ld: /tmp/hangman-3292ab.o: in function `append_and_order':
hangman.c:(.text+0x26c): undefined reference to `quicksort'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I compile them separately and link them together manually with
gcc -c hangman.c
gcc -c ../quicksort/quicksort.c
gcc -o hangman hangman.o quicksort.o
(As corrected). And that works, but isn't the point of #include to do this automatically at the preprocessing stage?

Compile non-mpi libraries with mpicc

I want to compile mpi with my own libraries. And I'm not sure if the options that work on gcc such as -I/ -L/ are available for mpicc.
I'm trying to compile with the following options but I get the following error.
mpicc -I$(CURRENT_DIR)/util -I$(CURRENT_DIR) -L$(CURRENT_DIR)/util -o server server.c
mpicc -I./util -I. -L./util -o server server.c
/tmp/ccA5be6Z.o: En la funciĆ³n `main':
server.c:(.text+0x195): undefined reference to `list_create'
server.c:(.text+0x219): undefined reference to `list_add'
server.c:(.text+0x228): undefined reference to `list_count'
collect2: error: ld returned 1 exit status
Those are my server.c includes
#include <mpi.h>
#include <list.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
And this is my pwd/util folder
$ ls
list.c list.h list.o
...
mpicc (and the other MPI build commands) are only wrappers on top of your compilers. So if you are using gcc, all the gcc options and directives are available.
Concerning OpenMPI, you can exhibit the compiler and the options used by using the --showme option. You can see the details in the OpenMPI FAQ (https://www.open-mpi.org/faq/?category=mpi-apps#wrapper-showme-with-no-file). Similar options are available in MPICH (https://www.mpich.org/static/docs/v3.2.x/www1/mpicc.html)
Your issue here is not related to MPI. You are just missing adding some objects (probably list.o) in the linking step. You should consider creating a Makefile to ease the build process.

undefined reference 'shm_open', already add -lrt flag here

I just have a system crash and reinstall Ubuntu 11.10, and my code produces this strange error.
I wrote a simple code sample to test where the problem is:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
int main (void) {
int i;
i = shm_open ("/tmp/shared", O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); printf ("shm_open rc = %d\n", i);
shm_unlink ("/tmp/shared");
return (0);
}
and the compile command is
gcc -lrt test.c -o test
The error is:
/tmp/ccxVIUiP.o: In function `main':
test.c:(.text+0x21): undefined reference to `shm_open'
test.c:(.text+0x46): undefined reference to `shm_unlink'
collect2: ld returned 1 exit status
I have already added -lrt lib, why does it still not compile?
Libraries at the end:
gcc test.c -o test -lrt
From GCC Link Options:
-llibrary
-l library
Search the library named library when linking.
(The second alternative with the library as a separate argument
is only for POSIX compliance and is not recommended.)
It makes a difference where in the command you write this option;
the linker searches and processes libraries and object files in the
order they are specified.
Thus, `foo.o -lz bar.o' searches library `z' after file foo.o but
before bar.o. If bar.o refers to functions in `z', those functions
may not be loaded.
Change the compile line from
gcc -lrt test.c -o test
to
gcc test.c -o test -lrt
In Expert C programming Page 108:
<Handy Heuristic>
Where to Put Library Options:Always put the -l library options at the rightmost end of your compilation command line.
But it doesn't tell why, so i guess this is somewhat a rule?:)
For those of you using super-auto-magical CMAKE like me, try to add:
target_link_libraries(your_binary_name PRIVATE librt.so)
to your CMakeLists.txt
Alternatively, replace PRIVATE for PUBLIC as needed..
If you have well established paths to libraries (e.g. all needed libs in /usr/lib), you may be fine with just stating in CMakeLists.txt:
set(CMAKE_CXX_FLAGS "-lrt")

GCC: How can I make this compiling and linking work?

I want to use from the file tester-1.c functions that I defined in libdrm.h and gave implementation in libdrm.c. The three files are on the same folder and use pthread functions.
Their include files are:
libdrm.h
#ifndef __LIBDRM_H__
#define __LIBDRM_H__
#include <pthread.h>
#endif
libdrm.c <- has no main()
#include <stdio.h>
#include <pthread.h>
#include "libdrm.h"
tester-1.c <- has teh main()
#include <stdio.h>
#include <pthread.h>
#include "libdrm.h"
The compiler error for libdrm.c says:
gcc libdrm.c -o libdrm -l pthread
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
And the compiler errors for tester-1.c say:
gcc tester-1.c -o tester1 -l pthread
/tmp/ccMD91zU.o: In function `thread_1':
tester-1.c:(.text+0x12): undefined reference to `drm_lock'
tester-1.c:(.text+0x2b): undefined reference to `drm_lock'
tester-1.c:(.text+0x35): undefined reference to `drm_unlock'
tester-1.c:(.text+0x3f): undefined reference to `drm_unlock'
/tmp/ccMD91zU.o: In function `thread_2':
tester-1.c:(.text+0x57): undefined reference to `drm_lock'
tester-1.c:(.text+0x70): undefined reference to `drm_lock'
tester-1.c:(.text+0x7a): undefined reference to `drm_unlock'
tester-1.c:(.text+0x84): undefined reference to `drm_unlock'
/tmp/ccMD91zU.o: In function `main':
tester-1.c:(.text+0x98): undefined reference to `drm_setmode'
tester-1.c:(.text+0xa2): undefined reference to `drm_init'
tester-1.c:(.text+0xac): undefined reference to `drm_init'
tester-1.c:(.text+0x10e): undefined reference to `drm_destroy'
tester-1.c:(.text+0x118): undefined reference to `drm_destroy'
All these functions are defined in libdrm.c
What gcc commands should I use to make these files compile and link?
To compile your .c sources to object files, use GCC's -c option. Then you can link the object files to an executable, against the required library:
gcc libdrm.c -c
gcc tester-1.c -c
gcc tester-1.o libdrm.o -o tester1 -lpthread
Doing compiling and linking in one go, as many others have suggested, works fine too. However, it's good to understand that the build process involves both of these stages.
Your build failed, because your translation modules (=source files) required symbols from each other.
libdrm.c alone couldn't produce an executable, because it does not have a main() function.
tester-1.c's linking failed, because the linker wasn't informed about the required symbols defined in libdrm.c.
With -c option, GCC compiles and assembles the source, but skips linking, leaving you with .o files, which can be linked into an executable or packaged into a library.
gcc tester-1.c libdrm.c -o tester1 -l pthread
You need to compile all the source files in one go rather than doing them individually. Either that or compile libdrm.c as a library and then link it with tester1.c when you compile that.
gcc test-1.c libdrm.c -o libdrm -l pthread

Resources