61

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) 

4 Answers 4

117

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.

6
  • what about the target... how do define your target then?
    – Lamda
    Oct 31, 2017 at 19:10
  • 1
    @Lamda Your question doesn't make sense to me, and this thread isn't about specifying the target but about linking against it. If you've got a specific problem you should consider searching SO and creating a new question if necessary.
    – Murphy
    Nov 2, 2017 at 9:16
  • 1
    My question was regarding this comment, which seemed to be well received but a bit odd to me. when you link your libraries, you link it to a target. add_executable would define the target. So how would you link to something that isn't defined yet?.. And sorry for bringing up an old topic (just wanted to learn something new...)
    – Lamda
    Nov 2, 2017 at 9:22
  • Read everything in context, first the question, then wRAR's answer, then mine. They address exactly this problem.
    – Murphy
    Nov 2, 2017 at 9:28
  • ohh.. Sorry.. I seem to have misread your answer. My initial thought was that you wanted to do it the other way ( link first then add_executable), hence my initial confusion, but you've written the exact opposite, which makes more sense.
    – Lamda
    Nov 2, 2017 at 13:33
54

The first argument of target_link_libraries is the target name:

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

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.

-3

Set you_lib_name before setting target_link_libraries

set(you_lib_name libname)
target_link_libraries(you_lib_name Qt5::Widgets Qt5::Core) 
1
  • 1
    Either your intention or code is wrong. set command defines the variable, but target_link_libraries wants the target. In CMake variables and targets are completely unrelated things.
    – Tsyvarev
    May 7, 2020 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.