shared Library linken



  • Hallo,

    ich wollte ein kleines Test Library erstellen, aber LD mag mich nicht:

    testapp.cpp:

    #include <stdio.h>
    #include "testlib.h"
    
    int main(int argc, char* argv[]) {
    
      double v1, v2, m;
      v1 = 5.2;
      v2 = 7.9;
    
      m  = mean(v1, v2);
    
      printf("The mean of %3.2f and %3.2f is %3.2f\n", v1, v2, m);
    
      return 0;
    }
    

    testlib.cpp:

    #include <stdio.h>
    
    double mean(double a, double b) {
      return (a+b) / 2;
     }
    

    Makefile:

    CC            = gcc
    CXX           = g++
    DEFINES       = -DUNICODE
    CFLAGSAPP     = -O2 -pipe -fomit-frame-pointer -Wall
    CFLAGSLIB     = -c -fPIC -march=prescott -O3 -pipe -Wall
    INCPATH       = -I './'
    LINK          =  g++
    LFLAGSAPP     = -L'./' -ltest
    LFLAGSLIB     = -shared -Wl,-soname,libtest.so.1
    LIBS          = 
    
    SOURCESLIB        = testlib.cpp
    OBJECTSLIB        = testlib.o
    DESTDIR_TARGETLIB = libtest.so.1.0.1
    
    SOURCESAPP        = testapp.cpp
    OBJECTSAPP        = testapp.o
    DESTDIR_TARGETAPP = testapp
    
    first: all
    
    all: $(DESTDIR_TARGETLIB) $(DESTDIR_TARGETAPP)
    
    $(testlib.o): testlib.cpp
    	$(CXX) -c $(CFLAGSLIB) $(INCPATH) -o $@ testlib.cpp
    
    $(DESTDIR_TARGETLIB):  $(testlib.o) 
    	$(LINK) $(LFLAGSLIB) -o $(DESTDIR_TARGETLIB) testlib.o $(LIBS)
    
    $(testapp.o): testapp.cpp
    	$(CXX) -c $(CFLAGSAPP) $(INCPATH) -o $@ testapp.cpp
    
    $(DESTDIR_TARGETAPP):  $(testapp.o) 
    	$(LINK) $(LFLAGSAPP) -o $(DESTDIR_TARGETAPP) testapp.o $(LIBS)
    

    Ergebnis:

    make
    g++ -L'./' -ltest -o testapp testapp.o
    /usr/lib/gcc/i686-pc-linux-gnu/4.3.4/../../../../i686-pc-linux-gnu/bin/ld: cannot find -ltest
    collect2: ld gab 1 als Ende-Status zurück
    make: *** [testapp] Fehler 1

    habt ihr ein Idee, warum das nicht geht?

    System Libs kann ich problemlos linken. Die Umgebungsvariablen habe ich auch testweise mal gesetzt, kein Erfolg 😞

    Viele Grüße



  • ln -s libtest.so.1.0.1 libtest.so
    g++ -L. -ltest ...
    


  • Hallo,

    danke für den Tipp! Ich musste zusätzlich noch

    libtest.so.1
    

    anlegen und

    export LD_LIBRARY_PATH=./
    

    , damit das funktionierte.

    Warum ist das denn so? Kann ich beim Linken auch gleich angeben, welche Version ich möchte? Bei den ganzen Beispielen im Internet ist das nicht erwähnt (Zumindest bei denen, die ich las, z.B http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html).

    Viele Grüße


Anmelden zum Antworten