18

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.

2 Answers 2

31

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 )
Sign up to request clarification or add additional context in comments.

Comments

12

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.