dynamische Bibliothek linken? wieso will das nicht
-
Waere ja alles so einfach, aber ich kriegs nicht hin:
// main.c #include <stdio.h> #include <sys/types.h> #include <unistd.h> extern pid_t getPid(); int main( int argc, char** argv ) { printf( "PID: %i\n", getPid() ); return 0; }
//libpid.c #include <sys/types.h> #include <unistd.h> pid_t getPid() { return getpid(); }
CC=gcc CFLAGS=-Wall LDFALGS= LIB_SRC=libpid.c LIB_OBJ=libpid.o LIB_NAME=libpid LIBRARY=$(LIB_NAME).so LIBFLAGS=-shared TEST_SRC=main.c TEST_OBJ=main.o TEST=main TESTFLAGS=-L. -l$(LIB_NAME) all: $(LIBRARY) $(TEST) $(LIBRARY): $(LIB_OBJ) $(CC) $(LIBFLAGS) $(LDFLAGS) $(LIB_OBJ) -o $(LIBRARY) $(LIB_OBJ): $(LIB_SRC) $(CC) $(LIBFLAGS) $(CFLAGS) $(LIB_SRC) -c $(TEST): $(TEST_OBJ) $(CC) $(TESTFLAGS) $(LDFLAGS) $(TEST_OBJ) -o $(TEST) $(TEST_OBJ): $(TEST_SRC) $(CC) $(CFLAGS) $(TEST_SRC) -c clean: rm -rf *.o *.so main
Kann das mal jemand ausprobieren und mir sagen warum er die Bibliothek nicht findet?
-
moe szyslak schrieb:
Kann das mal jemand ausprobieren
thomas@Majestix:~/Source/temp$ ls libpid.c main.c thomas@Majestix:~/Source/temp$ cat libpid.c //libpid.c #include <sys/types.h> #include <unistd.h> pid_t getPid() { return getpid(); } thomas@Majestix:~/Source/temp$ cat main.c // main.c #include <stdio.h> #include <sys/types.h> #include <unistd.h> extern pid_t getPid(); int main( int argc, char** argv ) { printf( "PID: %i\n", getPid() ); return 0; } thomas@Majestix:~/Source/temp$ gcc -shared -o libpid.so libpid.c thomas@Majestix:~/Source/temp$ gcc -L. -lpid -o main main.c thomas@Majestix:~/Source/temp$ ./main PID: 8524 thomas@Majestix:~/Source/temp$
moe szyslak schrieb:
und mir sagen warum er die Bibliothek nicht findet?
ld sucht nach dem Pattern "libXXX.{a|so}". Bei deinem Makefile täte er "liblibpid.so.so" suchen und diese nicht finden. Siehe dazu auch `man ld`:
ld manpage schrieb:
-larchive
--library=archive
Add archive file archive to the list of files to link. This option
may be used any number of times. ld will search its path-list for
occurrences of "libarchive.a" for every archive specified.On systems which support shared libraries, ld may also search for
libraries with extensions other than ".a". Specifically, on ELF
and SunOS systems, ld will search a directory for a library with an
extension of ".so" before searching for one with an extension of
".a". By convention, a ".so" extension indicates a shared library.Vielleicht interessiert dich auch der Parameter -h beim Erzeugen deines Objects oder die richtige[tm] Syntax für das Linken von Object-Files (anstatt Bibliotheken):
gcc -o main main.c libpid.so
HTH