简体   繁体   中英

Undefined reference while linking static C library

In my latest project I am encountering a strange issue regarding an undefined reference to a method of a shared library. I searched on SO but all I could find was either C++ related (extern "C") or not really helping.

The library in question is my fork of libosm which uses protobuf to generate de-/serialization code for OpenStreetMap data in its binary format (.osm.pbf). The function in question is osmpbf__blob__unpack but that is just the first I end up using so I suspect its a general problem.

I inspected the resulting libosm.a with nm and the method is there and exported but for some reason it is not found while linking. Below are my current flags. I tried changing the order and even including all libraries twice (as suggested in another thread) but I always end up with the undefined reference.

CFLAGS      = -v -std=c99 -O3 -Wall -Wextra -pedantic
LIBFLAGS    = -losmpbf -lprotobuf-c -lz -lpthread

At the moment I am quite lost on what the error could be, but I think it might be a minor general error. It has been a while since I used C.. Any help would be appreciated.

Cheers, Florian

Edit: Here is my complete Makefile. I just made up the name for the variable LIBFLAGS since I use my own little rule but it seems like I should use LDLIBS and the builtin rules for this simple case.

CC          = gcc
CFLAGS      = -v -std=c99 -O3 -Wall -Wextra -pedantic
LIBFLAGS    = -losmpbf -lprotobuf-c -lz -lpthread

all: main.x

main.x: main.c
    $(CC) $(CFLAGS) $(LIBFLAGS) main.c -o main.x

clean:
    rm -rf *.o main.x

The problem is the linker (gcc), like most linkers, processes the parameters from left to right. so the link sees the libraries, but there is no unresolved references to be handled, so nothing happens.

The fix is to place the libraries last on the line rather than just after the CFLAGS.

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