Search code examples
cmakeqt-creatorqt6

QT creator and CMake, cannot link to external library


Newbee here on QT creator and CMake and apologies for the basic question but struggling to link simple external library to my main project. Here are the project paths:

build-QMLTest-Desktop_Qt_6_3_1_MinGW_64_bit-Debug   // application binary path
QMLLib
    build-mylib-Desktop_Qt_6_3_1_MinGW_64_bit-Debug // lib binary path
    mylib
        CMakeLists.txt
        mylib.cpp   // lib source path
        mylib.h
QMLTest
    CMakeLists.txt
    main.cpp    // application source path

Here is my how I am trying to link the static library (snipped of QMLTest/CMakeLists.txt). I am using this thread as reference:

How do I explicitly specify an out-of-tree source in CMake?

add_subdirectory(../QMLLib/mylib ../QMLLib/build-mylib-Desktop_Qt_6_3_1_MinGW_64_bit-Debug)

target_link_libraries(mylib)

set(PROJECT_SOURCES
        main.cpp
        qml.qrc
)

main.cpp

#include "../QMLLib/mylib/mylib.h"

int main(int argc, char *argv[])
{
    Mylib mylib;

Yet, it won't resolve, here is the output

C:/Qt/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/QMLTest.dir/main.cpp.obj: in function `qMain(int, char**)':
C:/myroot/UITest/QMLTest/main.cpp:13: undefined reference to `Mylib::Mylib()'
collect2.exe: error: ld returned 1 exit status

I could not get an answer, so 50 points for grabs, since I cannot resolve this issue.

Ideally if you can point me out to recent project using CMake and QT Creator 8.0 making a simple project and library and link them together, it would be great.


Solution

  • I ended up re-arranging the folders and the solution is here:

    Cannot add library source to cmake project on QT

    where I just include the sources from a separate library project