From QMake to CMake. (Different path levels)

Hi evereyone,

I’m trying to port a multilevel project from QMake to CMake.

Below show I the desired structure:

The location of the root CMakeLists.txt a path level before than the main and the lib is a requirement

    ├── CMakeLists.txt
    ├── app02
    │   ├── CMakeLists.txt
    │   └── main.cpp
    └── lib
        ├── CMakeLists.txt
        ├── myprint.cpp
        └── myprint.h

The root CMakeLists.txt is so written:

    cmake_minimum_required(VERSION 3.13)

    set(CMAKE_PROJECT_NAME "testProject")
    project(${CMAKE_PROJECT_NAME})

    list(APPEND CMAKE_PREFIX_PATH "/home/enigma/Qt/5.15.2/gcc_64")

    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)

    set(CMAKE_AUTOMOC ON)
    set(CMAKE_INCLUDE_CURRENT_DIR ON)

    find_package(Qt5Core REQUIRED)
    find_package(Qt5Widgets REQUIRED)

    add_subdirectory(lib app02)

    set(TARGET "app02Build")
    add_executable(${TARGET} ${SOURCES})

    target_link_libraries(${TARGET} thePrintLibrary Qt5::Core Qt5::Widgets)

The main.cpp in app002 path is written so:

    #include "../lib/myprint.h"

    int main(int argc, char *argv[])
    {
        myPrint::print("This the second App");
    }

And here is its correspondig CMakeLists.txt:

    cmake_minimum_required(VERSION 3.13)

    set(SOURCES
        main.cpp
    )

The lib path contains the next files:

myprint.h

    #ifndef MYPRINT_H
    #define MYPRINT_H

    #include <QDebug>
    #include <string>

    class myPrint
    {
    public:
        static void print(std::string toPrint) {
            qDebug() << toPrint.c_str();
        }
    };

    #endif // MYPRINT_H

myPrint.cpp

    #include "myprint.h"
    //yes, there is no more code :-)

and the corresponding CMakeLists.txt

    cmake_minimum_required(VERSION 3.13)

    add_library( 
    	thePrintLibrary
    	myprint.h
    	myprint.cpp
    )

The CMake prescan runs fine, but when I compile I become the next error:

    [ 28%] Building CXX object app02/CMakeFiles/thePrintLibrary.dir/myprint.cpp.o
    In file included from /home/enigma/APD_Programas/Port/test/lib/myprint.cpp:1:
    /home/enigma/APD_Programas/Port/test/lib/myprint.h:4:10: fatal error: QDebug: No such file or directory
     #include <QDebug>
              ^~~~~~~~
    compilation terminated.
    gmake[2]: *** [app02/CMakeFiles/thePrintLibrary.dir/build.make:63: app02/CMakeFiles/thePrintLibrary.dir/myprint.cpp.o] Error 1
    gmake[1]: *** [CMakeFiles/Makefile2:162: app02/CMakeFiles/thePrintLibrary.dir/all] Error 2
    gmake: *** [Makefile:84: all] Error 2

Someone so kind to help me?

Thanks in advance

Thank you Claus,
I’ll take a look

Your thePrintLibrary doesn’t link against any Qt libraries. Try adding the following to its CMakeLists.txt file:

target_link_libraries(thePrintLibrary PUBLIC Qt5::Core)

Thank you Craig, that is!