无法链接 pthreads 来解决 cmake 编译问题

Not having ability to link pthreads to resolve cmake compiling issue

本文关键字:cmake 编译 问题 解决 链接 pthreads      更新时间:2023-10-16

这是编译时的输出,我达到 100% 然后当链接失败时,可能是由于缺少线程

cd /home/username/code/gnucash/build/src/gnc && /usr/bin/cmake -E cmake_link_script CMakeFiles/cutecash.dir/link.txt --verbose=1
/usr/bin/c++       CMakeFiles/cutecash.dir/moc_AccountItemModel.cxx.o CMakeFiles/cutecash.dir/moc_AccountSelectionDelegate.cxx.o CMakeFiles/cutecash.dir/moc_RecentFileMenu.cxx.o CMakeFiles/cutecash.dir/moc_SplitListModel.cxx.o CMakeFiles/cutecash.dir/moc_SplitListView.cxx.o CMakeFiles/cutecash.dir/moc_mainwindow.cxx.o CMakeFiles/cutecash.dir/moc_dashboard.cxx.o CMakeFiles/cutecash.dir/fpo/moc_FPO.cxx.o CMakeFiles/cutecash.dir/fpo/moc_ViewletModel.cxx.o CMakeFiles/cutecash.dir/fpo/moc_ViewletView.cxx.o CMakeFiles/cutecash.dir/qrc_gnucash.cxx.o CMakeFiles/cutecash.dir/qrc_gtk-icons.cxx.o CMakeFiles/cutecash.dir/qrc_fallback-icons.cxx.o CMakeFiles/cutecash.dir/qrc_stylesheets.cxx.o CMakeFiles/cutecash.dir/AccountItemModel.cpp.o CMakeFiles/cutecash.dir/AccountSelectionDelegate.cpp.o CMakeFiles/cutecash.dir/Cmd.cpp.o CMakeFiles/cutecash.dir/QofEventWrapper.cpp.o CMakeFiles/cutecash.dir/RecentFileMenu.cpp.o CMakeFiles/cutecash.dir/Session.cpp.o CMakeFiles/cutecash.dir/SplitListModel.cpp.o CMakeFiles/cutecash.dir/SplitListView.cpp.o CMakeFiles/cutecash.dir/main.cpp.o CMakeFiles/cutecash.dir/mainwindow.cpp.o CMakeFiles/cutecash.dir/mainwindow-file.cpp.o CMakeFiles/cutecash.dir/dashboard.cpp.o CMakeFiles/cutecash.dir/fpo/FPO.cpp.o CMakeFiles/cutecash.dir/fpo/ViewletModel.cpp.o CMakeFiles/cutecash.dir/fpo/ViewletView.cpp.o  -o cutecash -rdynamic ../backend/xml/libgnc-backend-xml.a ../import-export/libgnc-import.a ../app-utils/libapp-utils.a ../optional/gtkmm/liblibgncmod-gtkmm.a ../engine/libengine.a ../gnc-module/libgnc-module.a ../core-utils/libcore-utils.a ../libqof/libqof.a -lguile -lgmp -lcrypt -lm -lltdl -lglibmm-2.4 -lgobject-2.0 -lsigc-2.0 -lglib-2.0 -lgconf-2 -lglib-2.0 -lgthread-2.0 -lglib-2.0 -lgobject-2.0 -lglib-2.0 -lgmodule-2.0 -lglib-2.0 -lglib-2.0 -lxml2 -lQtGui -lQtCore -lgobject-2.0 -lsigc-2.0 -lgconf-2 -lgthread-2.0 -lgmodule-2.0 -lxml2 -lQtGui -lQtCore 
/usr/bin/ld: CMakeFiles/cutecash.dir/main.cpp.o: undefined reference to symbol 'pthread_getspecific@@GLIBC_2.2.5'
/usr/bin/ld: note: 'pthread_getspecific@@GLIBC_2.2.5' is defined in DSO /lib/x86_64-linux-gnu/libpthread.so.0 so try adding it to the linker command line
/lib/x86_64-linux-gnu/libpthread.so.0: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
make[2]: *** [src/gnc/cutecash] Error 1
make[2]: Leaving directory `/home/username/code/gnucash/build'
make[1]: *** [src/gnc/CMakeFiles/cutecash.dir/all] Error 2
make[1]: Leaving directory `/home/username/code/gnucash/build'
make: *** [all] Error 2
我尝试

了很多弄乱CMakeList文件,以下是我尝试过的一些事情:

  • 添加FIND_PACKAGE (Threads)
  • 添加TARGET_LINK_LIBRARIES(pthread)(导致 cmake 错误)

任何帮助将不胜感激,谢谢。

并将其添加到主(根)CMakeLists:

include_directories( ${ZLIB_INCLUDE_DIRS} )
FIND_PACKAGE (ZLIB REQUIRED)
FIND_PACKAGE (Threads)

src/gnc有一个子 CMakeLists。我添加了这个:

target_link_libraries ( cutecash ${CMAKE_THREAD_LIBS_INIT} )
target_link_libraries( cutecash ${ZLIB_LIBRARIES} )

它现在编译干净。