Reputation: 562
I'm experiencing some compilation issues with my dynamic library. It should be linked to main.c but for all references to it I receive "undefined reference to function_name".
The contents of main.c isn't really that important; I include my library:
#include "matrix.h"
Then I have a simple Makefile to link the two.
#Variables
LIB = matrix
# Usual compilation flags
CFLAGS = -std=c99 -Wall -Wextra -g
CPPFLAGS = -I../include -DDEBUG
LDFLAGS = -lm
# Special rules and targets
.PHONY: all clean help
all: $(LIB).o libmatrix.so main
$(LIB).o: $(LIB).c $(LIB).h
$(CC) $(CFLAGS) $(CPPFLAGS) -fPIC -c -o $@ $<
libmatrix.so: $(LIB).o
$(CC) $(CFLAGS) -fPIC -shared -o $@ $< $(LDFLAGS)
main: main.o libmatrix.so
$(CC) $(CFLAGS) -o $@ $< -L -lmatrix
Can anyone direct me to where I might be going wrong? Many thanks in advance.
Upvotes: 0
Views: 555
Reputation: 1
You probably want -L.
not -L
in your last line, so:
main: main.o libmatrix.so
$(CC) $(CFLAGS) -o $@ $< -L. -lmatrix
You should read Program Library HOWTO and Drepper's paper: How to Write Shared Libraries; you might want to set some -rpath
at link time (maybe using -Wl,-rpath,.
...), and you might want to link with -rdynamic
....
Alternatively, set your LD_LIBRARY_PATH
environment variable to contain .
(I don't recommend that), or install your shared library in /usr/local/lib/
(and add it to /etc/ld.so.conf
then run ldconfig
). See also dlopen(3), environ(7), ld.so(8), ldconfig(8)
Upvotes: 4