2013-05-20 39 views
8

首先,我看了一下this的帖子,找不到解決我的問題的方法。我試圖用兩個頭文件和鏈接與我的主要程序建立一個庫文件夾中,我的文件夾中容器它包括:CMake無法確定目標的鏈接器語言

linkedStack.h 
linkedQueue.h 

中的CMakeLists.txt在我的容器文件夾是

add_library(container linkedQueue.h linkedStack.h) 

install (TARGETS container DESTINATION bin) 
install (FILES linkedQueue.h linkedStack.h DESTINATION include) 

,而我在源目錄中的CMakeLists.txt是:

cmake_minimum_required(VERSION 2.6) 
project(wordLadder) 

# set version number 
set (MAJOR 1) 
set (MINOR 0) 

# configure header file to be placed in binary 
configure_file(
     "${PROJECT_SOURCE_DIR}/ladderConfig.h.in" 
     "${PROJECT_BINARY_DIR}/ladderConfig.h" 
) 

# add binary tree to search path for include files 
# so we can find config 
include_directories("${PROJECT_BINARY_DIR}") 

#add container library 
include_directories ("${PROJECT_SOURCE_DIR}/container") 
add_subdirectory(container) 

#add executable 
add_executable(wordLadder ladderMain.cpp) 
target_link_libraries (wordLadder container) 

install (TARGETS wordLadder DESTINATION bin) 
install (FILES "${PROJECT_BINARY_DIR}/ladderConfig.h" 
     DESTINATION include) 

和錯誤,我得到:

CMake Error: Cannot determine link language for target "container". 
CMake Error: CMake can not determine linker language for target:container 
-- Generating done 
-- Build files have been written to: /home/gmercer/Linux_dev/wordLadder/build 

我不知道我在做什麼錯在這裏,但我認爲這是與我的圖書館CMake的文件。

回答

10

您已添加創建container庫的目標。該目標只包含頭文件。請參閱CMake documentation

add_library:使用指定的源文件將庫添加到項目中。

add_library([STATIC | SHARED |模塊] [EXCLUDE_FROM_ALL] 源1源2 ... sourceN)

添加名爲從在命令調用列出的源文件建立一個圖書館的目標。對應於邏輯目標名稱,並且在項目中必須是全局唯一的。構建的庫的實際文件名是基於本地平臺的約定(如lib.a或.lib)構建的。

但是,你不能僅僅從沒有任何cpp文件的頭文件構建庫。這就是爲什麼你有這樣的錯誤。

+0

我應該如何去鏈接頭文件?他們都是模板類,所以不包含.cpp文件 –

+1

@ Need4Sleep你已經添加了'include_directories(「$ {PROJECT_SOURCE_DIR}/container」)'這樣編譯器即使沒有特殊的'容器'libarry也能找到你的頭文件。你不需要'容器'。 –

+0

我明白了,謝謝! –