1

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!

hooch
  • 1,135
  • 1
  • 16
  • 31
  • 1
    Nah. Includes have nothing to do with linker errors. Libraries do. You are not linking against a required library which contains the **definition** (as opposed to the **declaration**) of the function in question. –  Jul 04 '13 at 20:55
  • OP *is* linking to the library which contains the definition. He/she is just doing it in the wrong place on the command line. – Carl Norum Jul 04 '13 at 21:00

1 Answers1

1

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
Carl Norum
  • 219,201
  • 40
  • 422
  • 469