获取CMake错误:无法为不是由项目构建的目标指定链接库

use*_*872 24 cmake

我在我的代码中实现了CMake但是我收到了错误

"无法为目标"Qt5 :: Widgets"指定链接库,这些链接库不是由项目构建的".

以下是CMakeLists.txt的内容:

#Specify the version being used aswell as the language
cmake_minimum_required(VERSION 2.6)

#Name your project here
project(eCAD)

#Sends the -std=c++11 flag to the gcc compiler
ADD_DEFINITIONS(-std=c++11)

#This tells CMake to main.cpp and name it eCAD
add_executable(eCAD main.cpp)


#include the subdirectory containing our libs
add_subdirectory (gui)
include_directories(gui)

#include Qt directories
find_package(Qt5Widgets)
find_package(Qt5Core)
find_package(Qt5Designer)
SET(QT_USE_QTDESIGNER ON)

#link_libraries
target_link_libraries(Qt5::Widgets Qt5::Core) 
Run Code Online (Sandbox Code Playgroud)

Mur*_*phy 45

除了公认的答案:一个重要的细节是放置target_link_libraries 后的add_executable 并 find_package线,因此所有连接部件都是已知的.


wRA*_*RAR 21

第一个参数target_link_libraries是目标名称:

target_link_libraries(eCAD Qt5::Widgets Qt5::Core) 
Run Code Online (Sandbox Code Playgroud)


Tsy*_*rev 6

另外,不要将目标名称与项目名称混淆:

  • 命令project指定项目名称,但
  • 目标是使用add_executable,add_library或创建的目标add_custom_target。

错误消息是关于目标的。