Bonjour à tous,

Voilà ça fait un moment maintenant que je cherche. je l'ai fait un peu partout, bien que la même réponse soit donnée à chaque fois. Apparemment la réponse a marché avec d'autres personnes mais malheureusement pour moi le problème persiste.

mon projet se divise comme suit :
  • build
  • src
    • main.cpp
    • finddialog.h
    • finddialog.cpp
  • CMakeLists.txt


Voilà mon CMakeLists.txt
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
cmake_minimum_required(VERSION 2.6)
# set project's name
PROJECT( qtsample )
 
# with SET() command you can change variables or define new ones
# here we define SAMPLE_SRCS variable that contains a list of all .cpp files
# note that we don't need \ at the end of line
SET( SAMPLE_SRCS
     ./src/main.cpp
     ./src/finddialog.cpp
)
 
# another list, this time it includes all header files that should be treated with moc
SET( SAMPLE_MOC_HDRS
 ./src/finddialog.h
)
 
# enable warnings
ADD_DEFINITIONS( -Wall )
 
# this command finds Qt4 libraries and sets all required variables
# note that it's Qt4, not QT4 or qt4
FIND_PACKAGE( Qt4 REQUIRED )
 
# add some useful macros and variables
# (QT_USE_FILE is a variable defined by FIND_PACKAGE( Qt4 ) that contains a path to CMake script)
INCLUDE( ${QT_USE_FILE} )
 
# and finally this will run moc:
QT4_WRAP_CPP( SAMPLE_MOC_SRCS ${SAMPLE_MOC_HDRS} )
 
# we need this to be able to include headers produced by uic in our code
# (CMAKE_BINARY_DIR holds a path to the build directory, while INCLUDE_DIRECTORIES() works just like INCLUDEPATH from qmake)
INCLUDE_DIRECTORIES( ${CMAKE_BINARY_DIR} )
 
# here we instruct CMake to build "sample" executable from all of the source files
ADD_EXECUTABLE( qtsample ${SAMPLE_SRCS} ${SAMPLE_MOC_HDRS} )
 
# last thing we have to do is to tell CMake what libraries our executable needs,
# luckily FIND_PACKAGE prepared QT_LIBRARIES variable for us:
TARGET_LINK_LIBRARIES( qtsample ${QT_LIBRARIES} )
et l'erreur que j'obtiens :
CMakeFiles/qtsample.dir/src/main.cpp.o: In function `FindDialog::~FindDialog()':
main.cpp: (.text._ZN10FindDialogD2Ev[_ZN10FindDialogD5Ev]+0x13): undefined reference to `vtable for FindDialog'
main.cpp: (.text._ZN10FindDialogD2Ev[_ZN10FindDialogD5Ev]+0x1f): undefined reference to `vtable for FindDialog'
CMakeFiles/qtsample.dir/src/finddialog.cpp.o: In function `FindDialog::FindDialog(QWidget*)':
finddialog.cpp: (.text+0x5c): undefined reference to `vtable for FindDialog'
finddialog.cpp: (.text+0x6b): undefined reference to `vtable for FindDialog'
CMakeFiles/qtsample.dir/src/finddialog.cpp.o: In function `FindDialog::tr(char const*, char const*)':
finddialog.cpp: (.text._ZN10FindDialog2trEPKcS1_[FindDialog::tr(char const*, char const*)]+0x21): undefined reference to `FindDialog::staticMetaObject'
collect2: ld a retourné 1 code d'état d'exécution
make[2]: *** [qtsample] Erreur 1
make[1]: *** [CMakeFiles/qtsample.dir/all] Erreur 2
make: *** [all] Erreur 2
Merci.

[Edit]: Problème résolu, d'ailleurs l'erreur était dans le fait que j'ajoutais le ${SAMPLE_MOC_HDRS} dans ADD_EXECUTABLE, alors qu'il fallait mettre ${SAMPLE_MOC_SRCS}.