Précédent   Forum des professionnels en informatique > Systèmes > Autres systèmes > Mac
Mac Avant de poster: Lire La FAQ Mac; Voir la page Outils; Voir les tutoriels.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 08/09/2011, 09h56   #1
Invité de passage
 
Inscription : novembre 2007
Messages : 5
Détails du profil
Informations forums :
Inscription : novembre 2007
Messages : 5
Points : 0
Points : 0
Par défaut path library dynamique

Bonjour,

Nouveau sur Mac, je ne parviens pas à spécifier au moment de l'édition de liens le chemin qui sera utilisé pour chercher une library dynamique.

Quelque chose doit m'échapper... Mon OS est Snow Léopard. Par exemple :

Fichier foo/foo.hpp :

Code :
1
2
3
4
5
6
7
8
9
10
11
#ifndef _FOO_HPP
#define _FOO_HPP

#include <iostream>

using namespace std;

void foo();

#endif
Fichier foo/foo.cpp :
Code :
1
2
3
4
5
6
#include "foo.hpp"

void foo(){
    cout << "hello world." << endl;
}
Le fichier main/main.cpp :
Code :
1
2
3
4
5
6
#include "foo.hpp"

int main(){
    foo();
}
et voici les commandes que j'utilise :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
#creating foo library
cd foo
g++ -fPIC -c foo.cpp
g++ -dynamiclib -install_name libfoo.dylib -o libfoo.dylib foo.o
cd ..

#linking with foo library
cd main
g++ -fPIC -I../foo -c main.cpp
g++ -undefined dynamic_lookup main.o -L../foo -Wl,-rpath,../foo -lfoo -o main
./main
Jusqu'ici tout va bien, les commandes ont réussies.

Si je regarde avec otool :
Je vois que le chemin donné avec -rpath n'est pas pris en compte :
Code :
1
2
3
4
5
main:
        libfoo.dylib (compatibility version 0.0.0, current version 0.0.0)
        /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.9.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.11)
Si j'exécute :

La library n'est pas trouvée :

Code :
1
2
3
4
5
dyld: Library not loaded: libfoo.dylib
  Referenced from: (...)/main/./main
  Reason: image not found
Trace/BPT trap
Si maintenant je copie la library dans le répertoire d'éxecution, l'éxecution se déroule correctement.

Quelques infos supplémentaires :

Code :
1
2
3
4
5
6
7
8
9
10
uname -a
Darwin mp-57052 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386

g++ -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5666.3~6/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)
Quelle est la bonne option à utiliser? Merci d'avance à ceux qui pourront m'orienter vers la solution!

David
s@mson est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/09/2011, 12h41   #2
Membre Expert
 
Avatar de Ceylo
 
Étudiant
Inscription : janvier 2007
Messages : 1 196
Détails du profil
Informations personnelles :
Âge : 21
Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : janvier 2007
Messages : 1 196
Points : 1 310
Points : 1 310
Tout est dans le nom d'installation indiqué lors de l'édition des liens de ta bibliothèque.

En indiquant -install_name libfoo.dylib, alors ton exécutable cherchera "libfoo.dylib". Si tu mets -install_name /toto/libfoo.dylib, ton exécutable cherchera "/toto/libfoo.dylib". Si l'OS ne trouve pas la bibliothèque à cette adresse, il vérifiera également dans /usr/lib et /usr/local/lib.

Donc je ne sais pas où tu voudrais mettre ta bibliothèque par rapport à ton programme?

Enfin note que pour prendre effet, et que ton programme enregistre la bonne adresse, tu dois refaire la phase d'édition des liens de ton programme. Et -rpath ne fontionne pas de la même façon sous Mac OS X :
Code :
1
2
3
4
     -rpath path
                 Add path to the runpath search path list for image being cre-
                 ated.  At runtime, dyld uses the runpath when searching for
                 dylibs whose load path begins with @rpath/.
Code :
1
2
3
4
5
6
7
-install_name name
                 Sets an internal "install path" (LC_ID_DYLIB) in a dynamic
                 library. Any clients linked against the library will record
                 that path as the way dyld should locate this library.  If
                 this option is not specified, then the -o path will be used.
                 This option is also called -dylib_install_name for compati-
                 bility.
__________________
Cherche un(e ) développeur/se pour la version Windows et/ou Linux de sfeMovie! Puis pour aider sur le noyau une fois les portages au point
Blog à ne SURTOUT PAS visiter :p
Ceylo est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/09/2011, 14h24   #3
Invité de passage
 
Inscription : novembre 2007
Messages : 5
Détails du profil
Informations forums :
Inscription : novembre 2007
Messages : 5
Points : 0
Points : 0
Merci beaucoup Ceylo pour les explications, j'y comprends mieux!

Si ça peut être utile à quelqu'un, voici l'exemple précédent qui fonctionne,
(j'ai rajouté les Makefile).

.
|-- foo
| |-- foo.cpp
| |-- foo.hpp
| `-- Makefile
|-- lib
|-- main
| |-- main.cpp
| `-- Makefile
|-- Makefile
|-- Makefile.conf
`-- README


Bonne journée!


Fichier: README
Code :
1
2
3
4
5
Demonstrate how to create a single dynamic library containing a single function,
then create a executable that links this library.

Plese edit Makefile.conf and adjust INSTALL_DIR.
Fichier: Makefile
Code :
1
2
3
4
5
6
7
8
9
10
11
all:
	cd foo ; make all
	cd main ; make all

clean:
	cd foo ; make clean
	cd main ; make clean

test:
	cd main;  make test
Fichier: Makefile.conf
Code :
1
2
3
4
5
6
CC = g++
CCFLAGS = -fPIC
DYLIBFLAGS = -dynamiclib -install_name
LDFLAGS = -undefined dynamic_lookup
INSTALL_LIB = $(HOME)/dylib_demo/lib
Fichier: foo/foo.hpp
Code :
1
2
3
4
5
6
7
8
9
10
11
#ifndef _FOO_HPP
#define _FOO_HPP

#include <iostream>

using namespace std;

void foo();

#endif
Fichier: foo/foo.cpp
Code :
1
2
3
4
5
6
#include "foo.hpp"

void foo(){
    cout << "hello world." << endl;
}
Fichier: foo/Makefile
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
include ../Makefile.conf

LIB = $(INSTALL_LIB)/libfoo.dylib

all: $(LIB) 

$(LIB): foo.o
	$(CC) $(DYLIBFLAGS) $(LIB) -o $(LIB) $<

%.o: %.cpp
	$(CC) $(CCFLAGS) -c $<

clean:
	rm -f *.o $(LIB)
Fichier: main/main.cpp
Code :
1
2
3
4
5
6
#include "foo.hpp"

int main(){
    foo();
}
Fichier: main/Makefile
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
include ../Makefile.conf

EXEC = main

all: $(EXEC)

$(EXEC): main.o
	$(CC) $(LDFLAGS) $< -L$(INSTALL_LIB) -lfoo -o $@

%.o: %.cpp
	$(CC) $(CCFLAGS) -I../foo -c $<

test:
	./main

clean:
	rm -f *.o main
s@mson est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 22h34.


 
 
 
 
Partenaires

Hébergement Web