c++ - qt4 和 CMake 的链接器问题

标签 c++ qt cmake

我正在尝试用 CMake 做一个 Qt 项目(Qt 4.8.6),但是我的链接器有一些问题,我有点迷路了。

这是我对我的项目进行“制作”时得到的结果:

chasselas:build tfaux$ make VERBOSE=1
"/Applications/CMake 2.8-12.app/Contents/bin/cmake" -H/Users/tfaux/Documents/workspace/tempQt4 -B/Users/tfaux/Documents/workspace/tempQt4/build --check-build-system CMakeFiles/Makefile.cmake 0
"/Applications/CMake 2.8-12.app/Contents/bin/cmake" -E cmake_progress_start /Users/tfaux/Documents/workspace/tempQt4/build/CMakeFiles /Users/tfaux/Documents/workspace/tempQt4/build/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/addressbook.dir/build.make CMakeFiles/addressbook.dir/depend
cd /Users/tfaux/Documents/workspace/tempQt4/build && "/Applications/CMake 2.8-12.app/Contents/bin/cmake" -E cmake_depends "Unix Makefiles" /Users/tfaux/Documents/workspace/tempQt4 /Users/tfaux/Documents/workspace/tempQt4 /Users/tfaux/Documents/workspace/tempQt4/build /Users/tfaux/Documents/workspace/tempQt4/build /Users/tfaux/Documents/workspace/tempQt4/build/CMakeFiles/addressbook.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/addressbook.dir/build.make CMakeFiles/addressbook.dir/build
Linking CXX executable addressbook
"/Applications/CMake 2.8-12.app/Contents/bin/cmake" -E cmake_link_script CMakeFiles/addressbook.dir/link.txt --verbose=1
/usr/bin/c++    -Wl,-search_paths_first -Wl,-headerpad_max_install_names   CMakeFiles/addressbook.dir/main.cpp.o CMakeFiles/addressbook.dir/addressbook.cpp.o  -o addressbook -F/usr/local/Cellar/qt/4.8.6/lib  -framework QtOpenGL -framework QtGui -framework QtCore 
Undefined symbols for architecture x86_64:
  "AddressBook::staticMetaObject", referenced from:
      AddressBook::tr(char const*, char const*) in addressbook.cpp.o
  "vtable for AddressBook", referenced from:
      AddressBook::AddressBook(QWidget*) in addressbook.cpp.o
   NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [addressbook] Error 1
make[1]: *** [CMakeFiles/addressbook.dir/all] Error 2
make: *** [all] Error 2

这是addressbook.h: http://qt-project.org/doc/qt-4.8/tutorials-addressbook-fr-part1-addressbook-h.html

这是地址簿.cpp: http://qt-project.org/doc/qt-4.8/tutorials-addressbook-fr-part1-addressbook-cpp.html

这是 CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
LIST(APPEND CMAKE_PREFIX_PATH "/usr/local/Cellar/qt/4.8.6")

#Test Cmake file for Qt applications
SET(PROJECT_NAME addressbook)
PROJECT(${PROJECT_NAME})
Find_PACKAGE(Qt4 REQUIRED)
SET(QT_USE_MAIN true)

#Setting the sources of the project
SET(PROJECT_SOURCES
main.cpp
addressbook.cpp
)

SET(PROJECTS_HEADERS
main.h
addressbook.h
)

#Traitement du moc Qt
QT4_WRAP_CPP(PROJECT_HEADERS_MOC ${PROJECT_HEADERS})

#inclusions
INCLUDE_DIRECTORIES(${TULIP_INCLUDE_DIR} ${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS})

#creation de l'executable et liaison des librairies
ADD_EXECUTABLE(addressbook ${PROJECT_SOURCES} ${PROJECT_HEADERS_MOC})
TARGET_LINK_LIBRARIES(addressbook ${QT_LIBRARIES} ${QT_NETWORK_LIBRARY})
INSTALL(TARGETS addressbook DESTINATION bin)

欢迎任何帮助,谢谢!

最佳答案

  1. 在您的 cmake 代码中使用小写命令。它更具可读性。

  2. 你打错了。您设置了 PROJECTS_HEADERS 变量,但将 PROJECT_HEADERS 传递给 qt4_wrap_cpp。更喜欢使用通过在选择时突出显示相同的字符串来使其显而易见的编辑器。修正拼写错误会修复您的错误。

  3. 使用 CMAKE_AUTOMOC 而不是 qt4_wrap_cpp。 http://www.cmake.org/cmake/help/v3.0/manual/cmake-qt.7.html它从 CMake 2.8.6 开始可用。

关于c++ - qt4 和 CMake 的链接器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25996060/

相关文章:

c++ - 打印 std::string 的字节表示

c++ - 为背景图像设置比例

r - 在需要更新版本的 R 的 Ubuntu 10.04 上从源代码安装 RStudio

c++ - g++ 解析错误 ":"

c++ - 撤消堆栈在 Qt 中保持为空

CMake 无法解析运行时目录路径

C++ 项目在glewinit()之后崩溃

c++ - #ifndef C++ 中包含守卫的语法

c++ - 标准似乎支持(下面的代码片段编译)一个与类本身具有相同类型的静态数据成员

c++ - 是否可以为不同的 block 静态分配不同的共享内存?