I am trying to link a static library while creating my program executable using the below makefile..
IDIR =../inc
CC=g++ -g
CFLAGS=-I$(IDIR)
WFLAGS=-Wall -W
OFLAGS=-O3
DLINUX=-D_LINUX
ODIR=obj
LDIR =../lib
LIBS=-lm
_OBJ = testclient.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/testclient.o: testclient.c
$(CC) -c $< $(CFLAGS) -o $@
$(ODIR)/file2.o: file2.c
$(CC) -c $< $(CFLAGS) -o $@
testclient: $(OBJ)
$(CC) -o $@ $^ $(LIBS) -lccn -pthread
.PHONY: clean
clean:
rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~
I have tried everything available from changing the order of the ‘-lccn‘ parameter to checking whether the function exists in the library (nm libccn.a gives the required function ccn_create() in it). The error returned is :
obj/testclient.o: In function `main':
/root/testClient/src/testclient.c:91: undefined reference to `ccn_create()'
The library libccn.a is in /usr/local/lib. I have also tried changing the directory path and then using -L flag to look in that location. Doesn’t work either. 🙁 ..Any ideas as to how can i make it work ?
My guess is that
libccn.ais a C library and the header that you use are not designed to be imported by a C++ compiler (there is noextern "C" { }block surrounding the function definition).C++ supports function overloading by mangling name of function. When you put a function in a
extern "C" { }block, C++ disable name mangling (and thus disable overloading). Here, in your error message, the function mentioned isccn_create(). Notice the(), this means that the function type is known, and thus the named looked up was a mangled name.When you do
nm libccn.ayou see the real name, and it isccn_create. That is not a mangled name. So to fix this, you’ll need to surround the function definition in aexport "C" { }block. The easiest way to do that is to surround the#includein such a block.BTW, you can reproduce the error by doing this.