Poodar
Poodar

Reputation: 175

CMake error: 'target is not built by this project'

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.

Upvotes: 16

Views: 37848

Answers (2)

Tomaz Canabrava
Tomaz Canabrava

Reputation: 2408

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 )

Upvotes: 28

Tsyvarev
Tsyvarev

Reputation: 66118

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})

Upvotes: 11

Related Questions