Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 840
Poodar Avatar asked Apr 06 '17 03:04

Poodar


People also ask

What is CMake target?

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.

How do I add a library to Cmakelist?

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.

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

What is sources CMake?

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


2 Answers

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 )
like image 93
Tomaz Canabrava Avatar answered Sep 18 '22 08:09

Tomaz Canabrava


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})
like image 29
Tsyvarev Avatar answered Sep 19 '22 08:09

Tsyvarev