13

My CMakeLists.txt file is:

cmake_minimum_required(VERSION 3.7)
project(OpenCV_Basics)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_LIBS})
target_link_libraries(OpenCV_Basics )

add_executable(OpenCV_Basics ${SOURCE_FILES})

When I tried to compile the main.cpp, I got stucked.

CMake Error at CMakeLists.txt:10 (target_link_libraries):
  Cannot specify link libraries for target "OpenCV_Basics" which is not 
built
  by this project.

What's wrong?

I am working in Clion on Mac.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Poodar
  • 145
  • 1
  • 2
  • 7

2 Answers2

22

add_executable defines a target, but on your code you define a target after trying to compile it.

just change the position of those two lines:

  • first define the target

  • link the library.

like this

add_executable(OpenCV_Basics ${SOURCE_FILES})
target_link_libraries(OpenCV_Basics )
Tomaz Canabrava
  • 2,320
  • 15
  • 20
11

When any CMake command accepts target argument, it expects given target to be already created.

Correct usage:

# Create target 'OpenCV_Basics' 
add_executable(OpenCV_Basics ${SOURCE_FILES})
# Pass the target to other commands
target_link_libraries(OpenCV_Basics ${OpenCV_LIBRARIES})
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153