2014-07-03 72 views
2

我有這樣的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的帖子。但是,發佈的解決方案並未解決我的問題。從C進行輸出是:

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

編輯:我發現,這個問題是由於的CMakeLists.txt文件我包括目錄。那就是:

file(GLOB gl_nbody_HEADER 
    "*.h" 
) 

#add_executable(gl_nbody ${gl_nbody_HEADER}) 

有什麼奇怪的是,我在我的SRC類似的文件目錄的唯一區別是,它增加了.cpp文件,而不是.h文件。但是,當我嘗試將頭文件添加到我的項目時,出現鏈接錯誤。

回答

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 
)