1

I am trying to build a simple CMake project but I'm having problems understanding CMake or at least the error I get.

The project i made is divided in a couple directories. The main one has this cmake:

cmake_minimum_required(VERSION 3.7.2)
project(Raytracer)
set(CMAKE_CXX_STANDARD 14)

add_subdirectory(src)
set(COMMON_INCLUDES ${PROJECT_SOURCE_DIR}/inc)

From there my project splits in 2 folders src and inc. and in src i have the following cmake with the idea to global all subfolders:

FILE(GLOB sub-dirs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *)
FOREACH(dir ${sub-dirs})
    IF(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${dir})
        ADD_SUBDIRECTORY(${dir})
    ENDIF()
ENDFOREACH()

add_executable(raytracer main.cpp)

I also add the executable there which is in this main src folder. From there on i want to be able to make sub folders adding with their own Cmake files and linking those files to my executable. I have the following cmake:

set(OBJECTS
    asdf.cpp
    )

add_library(obj_files ${OBJECTS} ${COMMON_FILES})
target_link_libraries(raytracer obj_files)

but when I try to build I get the following error:

Cannot specify link libraries for target "raytracer" which is not built by this project.
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
user8087026
  • 11
  • 1
  • 2
  • 1
    You should **create a target** (`raytracer` in your case) **before use it** in `target_link_libraries` and other commands which expects targets. – Tsyvarev Jun 13 '18 at 22:16

1 Answers1

2

Basically in Cmake file or toolchain.cmake the order of commands are important! target_link_libraries() or target_include_directories() have to be always after add_executable()

NM Pennypacker
  • 6,704
  • 11
  • 36
  • 38