简体   繁体   English

从 QMake 到 CMake。 (不同的路径级别)

[英]From QMake to CMake. (Different path levels)

I'm trying to port a multilevel project from QMake to CMake.我正在尝试将多级项目从 QMake 移植到 CMake。 (I try to make it work a simple example first) (我首先尝试使其成为一个简单的示例)

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 的位置比 main 和 lib 之前的路径级别是一个要求

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

The root CMakeLists.txt is so written:根 CMakeLists.txt 是这样写的:

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(Qt5Widgets REQUIRED)
set(QT5_LIBRARIES Qt5::Widgets)

add_subdirectory(lib app02)

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

target_link_libraries(${TARGET} thePrintLibrary ${QT5_LIBRARIES})

The main.cpp in app002 path is written so: app002 路径中的 main.cpp 是这样写的:

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

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

And here is its correspondig CMakeLists.txt:这是其对应的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.13)

set(SOURCES
    main.cpp
)

The lib path contains the next files: lib 路径包含以下文件:

myprint.h 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和相应的 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: CMake 预扫描运行良好,但是当我编译时出现下一个错误:

[ 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提前致谢

Has drescherjm say it in the comment, you're missing Qt5::Core in your target_link_libraries drescherjm 在评论中说过吗,您的 target_link_libraries 中缺少 Qt5::Core

first add:首先添加:

find_package(Qt5Core REQUIRED)

then replace:然后替换:

target_link_libraries(${TARGET} thePrintLibrary ${QT5_LIBRARIES})

by经过

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

or replace或更换

set(QT5_LIBRARIES Qt5::Widgets)

by经过

set(QT5_LIBRARIES Qt5::Widgets Qt5::Core)

Thank you both,谢谢你们俩,

but when I replace the lines for:但是当我替换以下行时:

find_package(Qt5 REQUIRED COMPONENTS Widgets Core)
set(QT5_LIBRARIES Qt5::Core Qt5::Widgets)

I become the same 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>

I tried to add the next sentence too: (because I suspect that the library don't link right to Qt)我也尝试添加下一句:(因为我怀疑该库没有正确链接到 Qt)

target_link_libraries(thePrintLibrary ${QT5_LIBRARIES})

then I get the next weird error on final linking:然后我在最终链接中得到下一个奇怪的错误:

[100%] Linking CXX executable app02Build
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/app02Build.dir/build.make:88: app02Build] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:74: CMakeFiles/app02Build.dir/all] Error 2
gmake: *** [Makefile:84: all] Error 2

Thank you all, I found the last fail, with your help.谢谢大家,在你们的帮助下,我找到了最后一次失败。

In that path distribution, I must supply the complete main path in add_executable.在该路径分布中,我必须在 add_executable 中提供完整的主路径。

add_executable(${TARGET} app02/main.cpp)

Perhaps is there a better solution, so, if somebody want add something, please, you're wellcomed也许有更好的解决方案,所以,如果有人想添加一些东西,请,你很高兴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM