/*
Bonjour à tous,
je n'arrive pas à compiler le projet C++ suivant
ci-dessous les codes du projet "hello world !" avec trois fichiers
- le main.cpp à la racine
- un sous-dossier avec deux fichiers sources, destinés à faire une
petite bibliothèque partagée
avec GCC la compilation se passe bien (fichier Makefile plus bas)
jusqu'à l'édition de liens avec le main
qui me donne un message d'erreur "undefined reference to ..." tous mes objets
au secours
*/
main.cpp :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 --- Projet | +-- [ build-aux ] | | | +-- basic_window.cpp | +-- singleton.cpp +-- main.cpp | +-- Makefile
basic_window.cpp :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 #include <stdio.h> #include <iostream> #include "build-aux/basic_window.hpp" int main() { BasicWindow& win1 = Singleton<BasicWindow>::instance(); BasicWindow& win2 = Singleton<BasicWindow>::instance(); std::cout << &win1 << "|" << &win2 << std::endl ; win1.say_hello(); win2.say_goodbye(); // return 0; }
singleton.cpp :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 #include <stdio.h> #include <iostream> #include "basic_window.hpp" using namespace std ; BasicWindow::BasicWindow() { cout << "Creation" << endl ; } BasicWindow::~BasicWindow() { cout << "Destruction" << endl ; } void BasicWindow::say_goodbye() { printf("Goodbye !\n"); } void BasicWindow::say_hello() { printf("Hello !\n"); }
Makefile :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 #include <stdio.h> #include <iostream> #include "singletontpl.hpp" using namespace std ; template <class T> T Singleton<T>::m_i = T(); template <class T> T& Singleton<T>::instance() { return m_i; }
Code Makefile : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 RM := rm -rf # All Target all: libempty.so foobar # Each subdirectory must supply rules for building sources it contributes build-aux/%.o: build-aux/%.cpp /usr/bin/g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" libempty.so: build-aux/singletontpl.o build-aux/basic_window.o /usr/bin/g++ -shared -rdynamic "$<" -o "$@" -fPIC main.o: main.cpp /usr/bin/g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" foobar: main.o /usr/bin/g++ -Ibuild-aux -o "$@" "$<" -lempty -L. -Wl,-rpath,. -e main # Other Targets clean: -$(RM) foobar.o \ build-aux/basic_window.o \ build-aux/singletontpl.o \ libempty.so \ foobar .PHONY: all clean .SECONDARY:
/*
comme je le disais, ça compile ok jusqu'au linkage,
le message d'erreur recraché par gcc (ld) est :
*/
/*
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 Invoking: Cross G++ Linker /usr/bin/g++ -Ibuild-aux -o "foobar" "main.o" -lempty -L. -Wl,-rpath,. -e main main.o: In function `main': /home/centipede/Documents/1.workspace/singletontpl//main.cpp:26: undefined reference to `Singleton<BasicWindow>::instance()' Makefile:33: recipe for target 'foobar' failed /home/centipede/Documents/1.workspace/singletontpl//main.cpp:27: undefined reference to `Singleton<BasicWindow>::instance()' /home/centipede/Documents/1.workspace/singletontpl//main.cpp:29: undefined reference to `BasicWindow::say_hello()' /home/centipede/Documents/1.workspace/singletontpl//main.cpp:30: undefined reference to `BasicWindow::say_goodbye()' collect2: error: ld returned 1 exit status make: *** [foobar] Error 1
questions : - manque-t-il une ou des options de GCC quelque part ?
- manque-t-il qualque chose dans le Makefile ?
- manque-t-il quelque chose dans le source ?
- manque-t-il une configuration à gcc ?
d'avance merci ...
laglumie
*/
Partager