user3859872
user3859872

Reputation: 927

Getting a CMake Error: Cannot specify link libraries for target which is not built by the project

I am implementing CMake in my code but I'm getting the error

Cannot specify link libraries for target "Qt5::Widgets" which is not built by the project 

Below are the contents of the 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) 

Upvotes: 63

Views: 118220

Answers (4)

Tsyvarev
Tsyvarev

Reputation: 65860

Also, do not confuse target name with the project name:

  • a command project specifies a project name, but
  • a target is the one created with add_executable, add_library or add_custom_target.

The error message is about the target.

Upvotes: 15

jingjiang
jingjiang

Reputation: 9

Set you_lib_name before setting target_link_libraries

set(you_lib_name libname)
target_link_libraries(you_lib_name Qt5::Widgets Qt5::Core) 

Upvotes: -4

Murphy
Murphy

Reputation: 3999

In addition to the accepted answer: An important detail is to place target_link_libraries after the add_executable and find_package lines, so all linked components are known.

Upvotes: 126

wRAR
wRAR

Reputation: 25693

The first argument of target_link_libraries is the target name:

target_link_libraries(eCAD Qt5::Widgets Qt5::Core) 

Upvotes: 55

Related Questions