简体   繁体   中英

gcc exit with undefined reference to function in header file

I want to compile a small program which has a pretty straight forward makefile, but I seem unable to get it working. Maybe you can help me. The makefile has the following targets:

visca-cli: visca-cli.c libvisca_hl.o
    gcc -Wall -o visca-cli visca-cli.c /usr/local/lib/libvisca.so libvisca_hl.o

libvisca_hl.o: libvisca_hl.c
    gcc -Wall -c libvisca_hl.c

I can 'make libvisca_hl.o' successfully and create the .o file. But 'make visca-cli' fails with error messages like

libvisca_hl.c:(.text+0x468a): undefined reference to `VISCA_get_md_disptime'

for every single function defined in libvisca.h (here it's VISCA_get_md_disptime)

Here are the include sections from the various files (ommitting standard libraries):

In visca-cli.c:

#include "libvisca.h"
#include "libvisca_hl.h"

In libvisca_hl.c:

#include "libvisca_hl.h"

In libvisca_hl.h:

#include "libvisca.h"

All includes quoted with "" are present in the local directory where I run make and where all the sourcefiles are. There are no subfolders. So I guess the problem lies with the makefile? Any help appreciated!

The order of libraries and objects on your compilation/link command line matters. In your case, you just need to put the shared object at the end:

gcc -Wall -o visca-cli visca-cli.c libvisca_hl.o /usr/local/lib/libvisca.so

On most systems /usr/local/lib is already part of the standard library search path, so you could simplify further:

gcc -Wall -o visca-cli visca-cli.c libvisca_hl.o -lvisca

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM