无法确定项目的链接语言

时间:2014-07-03 18:53:52

标签: c++ cmake

我有这个CMakeLists.txt文件:

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

我不确定我做错了什么。我查看了this帖子。但是,发布的解决方案并未解决我的问题。 cmake的输出是:

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

编辑:我发现问题是由于 include 目录中的CMakeLists.txt文件造成的。这是:

file(GLOB gl_nbody_HEADER 
    "*.h"   
)

#add_executable(gl_nbody ${gl_nbody_HEADER})

对我来说,奇怪的是我在 src 目录中有一个类似的文件,唯一的区别是它添加.cpp文件而不是.h文件。但是,当我尝试将头文件添加到项目中时,我收到链接错误。

1 个答案:

答案 0 :(得分:2)

add_subdirectory表示添加包含CMakeLists.txt文件的子目录。如果您需要告诉CMake您的头文件放置位置需要使用include_directory

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

另外我建议你合并并更改你文件中的一些指令(我不确定FIND_PACKAGE指令,但我认为其他指令看起来是正确的):

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
)