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

C++ 从QMake到CMake。(不同的路径级别),c++,qt,cmake,C++,Qt,Cmake,我正在尝试将一个多层次项目从QMake移植到CMake。(我试着先用一个简单的例子来说明) 下面显示了所需的结构: 根CMakeLists.txt的位置比main和lib的路径级别更高,这是一项要求 ├── CMakeLists.txt ├── app02 │   ├── CMakeLists.txt │   └── main.cpp └── lib ├── CMakeLists.txt ├── myprint.cpp └── myprint.h 根CMakeLists

我正在尝试将一个多层次项目从QMake移植到CMake。(我试着先用一个简单的例子来说明)

下面显示了所需的结构:

根CMakeLists.txt的位置比main和lib的路径级别更高,这是一项要求

├── CMakeLists.txt
├── app02
│   ├── CMakeLists.txt
│   └── main.cpp
└── lib
    ├── CMakeLists.txt
    ├── myprint.cpp
    └── myprint.h
根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})
app002路径中的main.cpp是这样写的:

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

int main(int argc, char *argv[])
{
    myPrint::print("This the second App");
}
下面是相应的CMakeLists.txt:

cmake_minimum_required(VERSION 3.13)

set(SOURCES
    main.cpp
)
库路径包含以下文件:

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
以及相应的CMakeLists.txt

cmake_minimum_required(VERSION 3.13)
    
add_library( 
    thePrintLibrary
    myprint.h
    myprint.cpp
)
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
[28%]构建CXX对象app02/cmakfiles/thePrintLibrary.dir/myprint.cpp.o
在/home/enigma/APD_Programas/Port/test/lib/myprint.cpp中包含的文件中:1:
/home/enigma/APD_Programas/Port/test/lib/myprint.h:4:10:致命错误:QDebug:没有这样的文件或目录
#包括
^~~~~~~~
编译终止。
gmake[2]:***[app02/CMakeFiles/thePrintLibrary.dir/build.make:63:app02/CMakeFiles/thePrintLibrary.dir/myprint.cpp.o]错误1
gmake[1]:***[CMakeFiles/Makefile2:162:app02/CMakeFiles/thePrintLibrary.dir/all]错误2
gmake:**[Makefile:84:all]错误2
有这么好的人来帮我吗


提前感谢

drescherjm在评论中说,您的目标链接库中缺少Qt5::Core

首先添加:

find_package(Qt5Core REQUIRED)
然后替换:

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

或替换

set(QT5_LIBRARIES Qt5::Widgets)

谢谢你们两位

但是,当我替换以下行时:

find_package(Qt5 REQUIRED COMPONENTS Widgets Core)
set(QT5_LIBRARIES Qt5::Core Qt5::Widgets)
我犯了同样的错误:-(

然后我在最终链接上遇到了下一个奇怪的错误:

[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
谢谢大家,, 在你的帮助下,我发现了最后一次失败

在该路径分布中,我必须在add_可执行文件中提供完整的主路径

add_executable(${TARGET} app02/main.cpp)
也许有更好的解决方案,所以,如果有人想添加一些内容,
请,您很好

我想除了Qt5::WidgetsThank,您还需要Qt5::Core谢谢,我做到了,但不要改变结果:-(我想您还需要Qt5CoreHi的find_package(),谢谢。我不知道为什么我的答案比您的答案高,我在论坛上是新手。非常感谢,我做到了,但我得到了同样的错误:-(您是否添加了行“find_package(Qt5Core REQUIRED)”?我添加了:find_package(Qt5 REQUIRED COMPONENTS Widgets Core)集合(Qt5_LIBRARIES Qt5::Core Qt5::Widgets)…产生相同的错误:-(当你提供的答案应该是问题的解决方案时,你不应该问问题。相反,如果问题与原来的问题不同,你可能需要问一个全新的问题。在这两种情况下,都很难帮助你,因为你没有小的例子。你可以复制t的错误。)原始问题是一个小Qt程序,其中只包含一个文件和一个int main(),一个CMakeLists.txt来演示问题。
(.text+0x20):未定义的对
main'的引用是源代码中列出的包含
int main()
的文件。我认为您缺少一个add\u子目录()CMakeLists.txt文件将main.cpp添加到Source的位置我是新加入论坛的,有点困惑,请原谅。我对一个简单的文件没有问题。问题出在树结构上。我已经编写了所有文件的代码。我不知道我是否可以在论坛中附加一个.zip和代码,所以这里是:
find_package(Qt5 REQUIRED COMPONENTS Widgets Core)
set(QT5_LIBRARIES Qt5::Core Qt5::Widgets)
[ 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>
target_link_libraries(thePrintLibrary ${QT5_LIBRARIES})
[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
add_executable(${TARGET} app02/main.cpp)