0

I'm trying to compile a very simple hello world Qt/QML C++ project with cmake on Windows using Visual Studio. Even though I added the Qt dependencies in the CMakeLists.txt, it complains in the linking process about sobe Qt functions.

Here's my CMakeLists.txt

include_directories(${Qt5Widgets_INCLUDE_DIRS} ${QtQml_INCLUDE_DIRS})
add_definitions(${Qt5Widgets_DEFINITIONS} ${QtQml_DEFINITIONS} ${${Qt5Quick_DEFINITIONS}})

qt5_add_resources(QT_RESOURCES qml.qrc)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(PROJECT "rosek_qml")

find_package(Qt5 COMPONENTS Widgets Qml Quick REQUIRED)

project(${PROJECT})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fstrict-aliasing")

if(NOT DEFINED HEADERS)
    file(GLOB HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
endif()

if(NOT DEFINED SOURCES)
    file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
endif()

source_group("Header Files" FILES ${HEADERS})
source_group("Source Files" FILES ${SOURCES})

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../include")
add_executable(${PROJECT} ${HEADERS} ${SOURCES} ${QT_RESOURCES})

add_library(ctmedia_codec SHARED IMPORTED)
add_library(ctmedia_drm SHARED IMPORTED)
add_library(ctstream SHARED IMPORTED)
set_target_properties(ctmedia_codec PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../bin/libCTMedia_codec.so)
set_target_properties(ctmedia_drm PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../bin/libCTMedia_drm.so)
set_target_properties(ctstream PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../bin/libCTStream.so)

target_link_libraries(${PROJECT}
    Qt5::Widgets
    Qt5::Qml
    Qt5::Quick
    )

The cpp files compile, but I get these linking errors:

Error   LNK2019 unresolved external symbol "__declspec(dllimport) void __cdecl qt_assert(char const *,char const *,int)" (__imp_?qt_assert@@YAXPEBD0H@Z) referenced in function "public: struct QTypedArrayData<unsigned short> * __cdecl QStaticStringData<13>::data_ptr(void)const " (?data_ptr@?$QStaticStringData@$0N@@@QEBAPEAU?$QTypedArrayData@G@@XZ)    rosek_qml   C:\Users\LZ\Desktop\roosek_ocr\src\main.obj 1  
Error   LNK2019 unresolved external symbol "__declspec(dllimport) public: __cdecl QString::QString(class QString const &)" (__imp_??0QString@@QEAA@AEBV0@@Z) referenced in function "public: class QString __cdecl <lambda_fbea028be4a75d219511fe3f4e1d52f5>::operator()(void)const " (??R<lambda_fbea028be4a75d219511fe3f4e1d52f5>@@QEBA?AVQString@@XZ)    rosek_qml   C:\Users\LZ\Desktop\roosek_ocr\src\main.obj 1  
Error   LNK2019 unresolved external symbol "__declspec(dllimport) public: __cdecl QString::~QString(void)" (__imp_??1QString@@QEAA@XZ) referenced in function main  rosek_qml   C:\Users\LZ\Desktop\roosek_ocr\src\main.obj 1  
Error   LNK2019 unresolved external symbol "__declspec(dllimport) public: __cdecl QString::QString(struct QStringDataPtr)" (__imp_??0QString@@QEAA@UQStringDataPtr@@@Z) referenced in function "public: class QString __cdecl <lambda_fbea028be4a75d219511fe3f4e1d52f5>::operator()(void)const " (??R<lambda_fbea028be4a75d219511fe3f4e1d52f5>@@QEBA?AVQString@@XZ) rosek_qml   C:\Users\LZ\Desktop\roosek_ocr\src\main.obj 1  
Error   LNK2019 unresolved external symbol "__declspec(dllimport) public: __cdecl QApplication::QApplication(int &,char * *,int)" (__imp_??0QApplication@@QEAA@AEAHPEAPEADH@Z) referenced in function main  rosek_qml   C:\Users\LZ\Desktop\roosek_ocr\src\main.obj 1  
Error   LNK2019 unresolved external symbol "__declspec(dllimport) public: virtual __cdecl QApplication::~QApplication(void)" (__imp_??1QApplication@@UEAA@XZ) referenced in function main   rosek_qml   C:\Users\LZ\Desktop\roosek_ocr\src\main.obj 1  
Error   LNK2019 unresolved external symbol "__declspec(dllimport) public: static int __cdecl QApplication::exec(void)" (__imp_?exec@QApplication@@SAHXZ) referenced in function main    rosek_qml   C:\Users\LZ\Desktop\roosek_ocr\src\main.obj 1  
Error   LNK2019 unresolved external symbol "__declspec(dllimport) public: __cdecl QUrl::QUrl(class QString const &,enum QUrl::ParsingMode)" (__imp_??0QUrl@@QEAA@AEBVQString@@W4ParsingMode@0@@Z) referenced in function main   rosek_qml   C:\Users\LZ\Desktop\roosek_ocr\src\main.obj 1  
Error   LNK2019 unresolved external symbol "__declspec(dllimport) public: __cdecl QUrl::~QUrl(void)" (__imp_??1QUrl@@QEAA@XZ) referenced in function main   rosek_qml   C:\Users\LZ\Desktop\roosek_ocr\src\main.obj 1  
Error   LNK2019 unresolved external symbol "__declspec(dllimport) public: __cdecl QQmlApplicationEngine::QQmlApplicationEngine(class QObject *)" (__imp_??0QQmlApplicationEngine@@QEAA@PEAVQObject@@@Z) referenced in function main rosek_qml   C:\Users\LZ\Desktop\roosek_ocr\src\main.obj 1  
Error   LNK2019 unresolved external symbol "__declspec(dllimport) public: virtual __cdecl QQmlApplicationEngine::~QQmlApplicationEngine(void)" (__imp_??1QQmlApplicationEngine@@UEAA@XZ) referenced in function main    rosek_qml   C:\Users\LZ\Desktop\roosek_ocr\src\main.obj 1  
Error   LNK2019 unresolved external symbol "__declspec(dllimport) public: void __cdecl QQmlApplicationEngine::load(class QUrl const &)" (__imp_?load@QQmlApplicationEngine@@QEAAXAEBVQUrl@@@Z) referenced in function main  rosek_qml   C:\Users\LZ\Desktop\roosek_ocr\src\main.obj 1  
Error   LNK2019 unresolved external symbol "bool __cdecl qRegisterResourceData(int,unsigned char const *,unsigned char const *,unsigned char const *)" (?qRegisterResourceData@@YA_NHPEBE00@Z) referenced in function "int __cdecl qInitResources_qml(void)" (?qInitResources_qml@@YAHXZ)   rosek_qml   C:\Users\LZ\Desktop\roosek_ocr\src\qrc_qml.obj  1  
Error   LNK2019 unresolved external symbol "bool __cdecl qUnregisterResourceData(int,unsigned char const *,unsigned char const *,unsigned char const *)" (?qUnregisterResourceData@@YA_NHPEBE00@Z) referenced in function "int __cdecl qCleanupResources_qml(void)" (?qCleanupResources_qml@@YAHXZ) rosek_qml   C:\Users\LZ\Desktop\roosek_ocr\src\qrc_qml.obj  1  
Error   LNK1120 14 unresolved externals rosek_qml   C:\Users\LZ\Desktop\roosek_ocr\src\Debug\rosek_qml.exe  1

All the errors here talk about using qmake Qt Unresolved external symbol LNK2019, but I want to use pure cmake.

I also tried building with mingw here: MinGw can't compile test file from cmake but I get these other errors. Any of the two will be good if anyone cal help me.

UPDATE: this question is not answered in the question masked as duplicate, the same CMakeLists work on Linux, I'm linking them correctly, please reopen

Guerlando OCs
  • 1,886
  • 9
  • 61
  • 150
  • You are missing a link to Qt::Core and probably others. – drescherjm Feb 16 '20 at 17:35
  • @drescherjm I added Qt5::Core and I still have the same quantity of errors. I didn't add any other things because the same CMake file works on Linux so I guesses the linked libs were ok. Do you have any ideas? – Guerlando OCs Feb 16 '20 at 17:56
  • @ JesperJuhl: That "generic" question about undefined reference is useful duplicate target for the code which doesn't link the library at all. But given question **actually links the library**, and the problem most likely is in a CMake plane, which that generic question doesn't cover. If you are not very familiar with CMake, just do not close (as a duplicate) CMake questions about "undefined reference" which have `target_link_libraries` call. – Tsyvarev Feb 16 '20 at 19:15
  • Seems you try to link against static libs? (mingw81_64\lib\libQt5Core.a). Do you have also some includes which rely on Q_DECL_IMPORT ? Can you remove f.e. QT_CORE_LIB or QT_QUICK_LIB from definitions? – valbok May 31 '23 at 16:14

0 Answers0