2

I have this CMakeLists.txt file:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
CMAKE_POLICY(SET CMP0002 OLD)

PROJECT(gl_nbody C CXX)

ADD_EXECUTABLE(gl_nbody src/main.cpp)
set_target_properties(gl_nbody PROPERTIES LINKER_LANGUAGE CXX)

INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(gl_nbody ${SDL2_LIBRARIES})

find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS})
target_link_libraries(gl_nbody ${OPENGL_LIBRARIES})

add_subdirectory("src")
add_subdirectory("include")

I'm not exactly sure what I am doing wrong. I have looked at this post. However, the solutions posted have not solved my issue. The output from cmake is:

-- Configuring done
CMake Error: CMake can not determine linker language for target:gl_nbody
CMake Error: Cannot determine link language for target "gl_nbody".
-- Generating done
-- Build files have been written to: /home/jared/projects/gl_nbody

Edit: I have found that the issue is due to the CMakeLists.txt file in my include directory. Here it is:

file(GLOB gl_nbody_HEADER 
    "*.h"   
)

#add_executable(gl_nbody ${gl_nbody_HEADER})

What is strange to me is that I have a similar file in my src directory whose only difference is that it adds .cpp files instead of .h files. However, I get a link error when I try to add header files to my project.

Community
  • 1
  • 1
user3285713
  • 151
  • 6
  • 14

1 Answers1

2

add_subdirectory means add subdirectory which contain CMakeLists.txt file. When you need tell CMake about where your header files placed you need use include_directory:

include_directories (
    ${OPENGL_INCLUDE_DIRS}
    ${SDL2_INCLUDE_DIRS}
    ${PROJECT_SOURCE_DIR}/include
)

Also I'd propose you merge and change some of directives in your files like this (I'm not sure about FIND_PACKAGE directives, but rest of directives in my opinion looks like correct):

CMAKE_MINIMUM_REQUIRED (VERSION 2.8)
CMAKE_POLICY           (SET CMP0002 OLD)

FIND_PACKAGE      (OpenGL REQUIRED)
FIND_PACKAGE      (SDL    REQUIRED)

PROJECT (gl_nbody C CXX)

INCLUDE (FindPkgConfig)

INCLUDE_DIRECTORIES (
    ${SDL2_INCLUDE_DIRS}
    ${OPENGL_INCLUDE_DIRS}
    ${PROJECT_SOURCE_DIR}/include
)

SET (
    gl_nbody_SRS
    ${PROJECT_SOURCE_DIR}/src/main.cpp
)

ADD_EXECUTABLE (
    gl_nbody
    ${gl_nbody_SRS}
)

TARGET_LINK_LIBRARIES (
    gl_nbody
    ${OPENGL_LIBRARIES}
    ${SDL2_LIBRARIES}
)

SET_TARGET_PROPERTIES (
    gl_nbody
    PROPERTIES LINKER_LANGUAGE CXX
)
Gluttton
  • 5,739
  • 3
  • 31
  • 58