用cmake链接外部库

时间:2019-05-07 09:54:24

标签: c++ cmake linker g++ mingw-w64

当我链接到外部库时,尽管这些函数在外部库中,但我还是得到了很多“未定义的引用”。

$ g++.exe -std=c++11 -g2 -O0 -Wall -Wextra -pedantic -g -Wl,--whole-archive E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a -Wl,--no-whole-archive -o buergerkarte.exe -Wl,--major-image-version,0,--minor-image-version,0 E:/msys64/mingw64/lib/libQt5Widgetsd.dll.a E:/msys64/usr/src/OpenSC-GUI/lib/opensc/lib/windows/libopensc.a -lz -lssl -lcrypto -llzma -Wl,-subsystem,windows E:/msys64/mingw64/lib/libQt5Guid.dll.a E:/msys64/mingw64/lib/libQt5Cored.dll.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(mainwidget.cpp.obj): In function `MainWidget::pksc15PinChangeDone(Error)':
E:/msys64/usr/src/OpenSC-GUI/src/mainwidget.cpp:392: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(mainwidget.cpp.obj): In function `MainWidget::pksc15PinUnblockDone(Error)':
E:/msys64/usr/src/OpenSC-GUI/src/mainwidget.cpp:410: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::cancel()':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:30: undefined reference to `sc_cancel'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::initScContext()':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:73: undefined reference to `sc_context_create'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:75: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::connectCard(bool)':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:92: undefined reference to `sc_detect_card_presence'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:96: undefined reference to `sc_wait_for_event'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:98: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:106: undefined reference to `sc_connect_card'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:109: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:114: undefined reference to `sc_lock'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:116: undefined reference to `sc_disconnect_card'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:118: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::connectReader(bool)':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:131: undefined reference to `sc_ctx_get_reader_count'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:138: undefined reference to `sc_wait_for_event'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:140: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:143: undefined reference to `sc_ctx_detect_readers'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:145: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:150: undefined reference to `sc_ctx_get_reader_count'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:151: undefined reference to `sc_ctx_get_reader'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:152: undefined reference to `sc_detect_card_presence'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:162: undefined reference to `sc_ctx_get_reader'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:167: undefined reference to `sc_ctx_get_reader_count'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::verifyCurrentSmartCard()':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:184: undefined reference to `sc_detect_card_presence'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:195: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::verifyCurrentCardReader()':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:207: undefined reference to `sc_ctx_detect_readers'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:207: undefined reference to `sc_ctx_get_reader_count'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:207: undefined reference to `sc_ctx_get_reader_by_name'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::cleanupScContext()':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:224: undefined reference to `sc_release_context'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::cleanupSmartCard()':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:233: undefined reference to `sc_unlock'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:234: undefined reference to `sc_disconnect_card'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::gatherPkcs15PinInfo()':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:246: undefined reference to `sc_pkcs15_bind'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:248: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:257: undefined reference to `sc_pkcs15_get_objects'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:259: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:260: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:262: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:275: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:280: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::changePinPkcs15(unsigned char const*, unsigned char const*)':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:290: undefined reference to `sc_pkcs15_bind'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:292: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:297: undefined reference to `sc_pkcs15_get_objects'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:299: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:301: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:308: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:315: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:325: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:329: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj):E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:335: more undefined references to `sc_pkcs15_unbind' follow
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::changePinPkcs15(unsigned char const*, unsigned char const*)':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:344: undefined reference to `sc_pkcs15_change_pin'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:348: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:353: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::unblockPinPkcs15(unsigned char const*, unsigned char const*)':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:363: undefined reference to `sc_pkcs15_bind'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:365: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:370: undefined reference to `sc_pkcs15_get_objects'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:372: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:374: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:381: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:387: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:391: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:397: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj):E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:401: more undefined references to `sc_pkcs15_unbind' follow
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::unblockPinPkcs15(unsigned char const*, unsigned char const*)':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:406: undefined reference to `sc_pkcs15_unblock_pin'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:410: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:415: undefined reference to `sc_pkcs15_unbind'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::getPersonalDatafromCard(sc_pkcs15_card*)':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:428: undefined reference to `sc_pkcs15_get_objects'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:430: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:438: undefined reference to `sc_format_oid'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:439: undefined reference to `sc_compare_oid'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:454: undefined reference to `sc_pkcs15_read_data_object'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:456: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:471: undefined reference to `sc_pkcs15_free_data_object'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:487: undefined reference to `sc_pkcs15_free_data_object'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:493: undefined reference to `sc_pkcs15_free_data_object'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:500: undefined reference to `sc_pkcs15_free_data_object'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:511: undefined reference to `sc_pkcs15_free_data_object'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::getSerialDatafromCard(sc_pkcs15_card*)':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:530: undefined reference to `sc_format_path'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::getX509CertificateDatafromCard(sc_pkcs15_card*)':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:562: undefined reference to `sc_format_path'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:566: undefined reference to `sc_select_file'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:568: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:576: undefined reference to `sc_pkcs15_read_certificate'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:578: undefined reference to `sc_strerror'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:582: undefined reference to `sc_pkcs15_free_certificate'
E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a(cardcontrolhandler.cpp.obj): In function `CardControlHandler::readDataFromFile(sc_pkcs15_card const*, sc_path const*, unsigned char*, unsigned long long)':
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:592: undefined reference to `sc_select_file'
E:/msys64/usr/src/OpenSC-GUI/src/cardcontrolhandler.cpp:596: undefined reference to `sc_read_binary'
collect2.exe: error: ld returned 1 exit status

这是我的CMakeLists.txt,它生成上面的命令:

# Bring header files into scope
set( LIB_DIR ${PROJECT_SOURCE_DIR}/lib )
include_directories(${LIB_DIR}/opensc/include)
include_directories (${LIB_DIR}/opensc/include/common)
include_directories(${LIB_DIR}/opensc/include/libopensc)
include_directories(${LIB_DIR}/opensc/include/scconf)

find_package(Qt5 REQUIRED COMPONENTS Core Widgets)
SET(QT_USE_QTSVG TRUE)

find_package( ZLIB REQUIRED )
include_directories( ${ZLIB_INCLUDE_DIRS} )

find_package(OpenSSL REQUIRED)
include_directories( ${OPENSSL_INCLUDE_DIRS} )


#Adding forms
set( FORMS forms/mainwidget.ui forms/pukdialog.ui forms/aboutdialog.ui) 

#Adding source files
set(SRCS main.cpp)
set(SRCS ${SRCS} mainwidget.cpp cardcontrolhandler.cpp error.cpp logger.cpp statusbar.cpp pukdialog.cpp aboutdialog.cpp x509certificatehandler.cpp )

#Adding headers
set(HEADERS mainwidget.h cardcontrolhandler.h statusbar.h logger.h pukdialog.h aboutdialog.h )

#Adding Resources
set(RESOURCES ../buergerkarte.qrc)

#Generation of Qt Stuff (moc files, ui headers and resource files)
QT5_WRAP_CPP( HEADERS_MOC ${HEADERS})
QT5_WRAP_UI( FORMS_HEADERS ${FORMS})
QT5_ADD_RESOURCES( RESOURCES_RCC ${RESOURCES})

#We have to include the binary directory because the qt stuff like ui headers will be generated there
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})

#Creates executable from sources, moc headers (Qt), forms headers (Qt) and resources (Qt)
add_executable (${PROJECT_NAME} ${SRCS} ${HEADERS_MOC} ${FORMS_HEADERS} ${RESOURCES_RCC} ../buergerkarte.rc )

#Linking with special arguments for windows or linux platform
if( WIN32 )
  set(LIBOPENSC ${LIB_DIR}/opensc/lib/windows/libopensc.a)
#  target_link_libraries(${PROJECT_NAME} ${QT_QTMAIN_LIBRARY} ${QT_LIBRARIES} ${LIBOPENSC} ${ZLIB_LIBRARIES} ${OPENSSL_LIBRARIES} ${CMAKE_DL_LIBS} -llcms -llzma -Wl,-subsystem,windows )  
  target_link_libraries(${PROJECT_NAME} ${QT_QTMAIN_LIBRARY} Qt5::Widgets ${LIBOPENSC} ${ZLIB_LIBRARIES} ${OPENSSL_LIBRARIES} ${CMAKE_DL_LIBS} -llzma -Wl,-subsystem,windows )  
elseif( APPLE )
  set(LIBOPENSC ${LIB_DIR}/opensc/lib/mac_osx/libopensc.3.dylib)
  #set(LIBOPENSC ./libopensc.3.dylib)
  target_link_libraries(${PROJECT_NAME} Qt5::Widgets ${LIBOPENSC}  ${ZLIB_LIBRARIES} ${OPENSSL_LIBRARIES} ${CMAKE_DL_LIBS} )
else()
  set(LIBOPENSC ${LIB_DIR}/opensc/lib/linux/libopensc.a)
  target_link_libraries(${PROJECT_NAME} Qt5::Widgets ${LIBOPENSC} ${ZLIB_LIBRARIES} ${OPENSSL_LIBRARIES} ${CMAKE_DL_LIBS} )
  set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
  ADD_CUSTOM_COMMAND (
    TARGET ${PROJECT_NAME} POST_BUILD 
    COMMAND lupdate -silent -I ../src ../src -ts ../translations/${PROJECT_NAME}_de.ts ../translations/${PROJECT_NAME}_it.ts
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  )

endif()

所有未定义的引用都包含在$ {LIB_DIR} /opensc/lib/windows/libopensc.a文件中。主程序从opensc添加头文件。尽管链接器已添加到命令中,但为什么它却看不到外部库中的功能?

g++.exe -std=c++11 -g2 -O0 -Wall -Wextra -pedantic -g -Wl,--whole-archive E:/msys64/usr/src/OpenSC-GUI/build/src/CMakeFiles/buergerkarte.dir/objects.a -Wl,--no-whole-archive -o buergerkarte.exe -Wl,--major-image-version,0,--minor-image-version,0 E:/msys64/mingw64/lib/libQt5Widgetsd.dll.a E:/msys64/usr/src/OpenSC-GUI/lib/opensc/lib/windows/libopensc.a -lz -lssl -lcrypto -llzma -Wl,-subsystem,windows E:/msys64/mingw64/lib/libQt5Guid.dll.a E:/msys64/mingw64/lib/libQt5Cored.dll.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32

我正在使用mingw-w64。谢谢!

0 个答案:

没有答案