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.
A CMake-based buildsystem is organized as a set of high-level logical targets. Each target corresponds to an executable or library, or is a custom target containing custom commands.
To add a library in CMake, use the add_library() command and specify which source files should make up the library. Rather than placing all of the source files in one directory, we can organize our project with one or more subdirectories.
Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.
This specifies the list of paths to source files for the target. The following commands all set or add to the SOURCES target property and are the usual way to manipulate it: add_executable() add_library()
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 )
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})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With