2015-04-01 56 views
1

我非常新的CMake和這裏的文件是在我的項目的根目錄我的CMakeLists.txt文件如何指定依賴於其他文件

cmake_minimum_required (VERSION 2.6) 
project (Tools C) 

set(CMAKE_C_FLAGS "-ansi -pedantic -Wall -Werror") 
include_directories("include") 

SET_SOURCE_FILES_PROPERTIES(lib/xstr.c PROPERTIES 
          OBJECT_DEPENDS "lib/xalloc.c") 

SET_SOURCE_FILES_PROPERTIES(lib/counter.c PROPERTIES 
          OBJECT_DEPENDS "lib/xstr.c") 

SET_SOURCE_FILES_PROPERTIES(lib/dynamic_array.c PROPERTIES 
          OBJECT_DEPENDS "lib/xalloc.c") 

SET_SOURCE_FILES_PROPERTIES(lib/list.c PROPERTIES 
          OBJECT_DEPENDS "lib/xalloc.c") 



add_executable(cat cat.c lib/xalloc.c lib/xfopen.c) 
add_executable(counter counter.c lib/counter.c) 
add_executable(darr dynamic_array.c lib/dynamic_array.c) 
add_executable(linked list.c lib/list.c) 

我把C文件包含在我的根main()功能目錄。我將其他c文件保存在{project_root}/lib目錄中。

我的問題是,我收到以下錯誤:

[ 33%] Built target cat 
mingw32-make.exe[2]: *** No rule to make target 'lib/xstr.c', needed by 'CMakeFi 
les/counter.dir/lib/counter.c.obj'. Stop. 
CMakeFiles\Makefile2:94: recipe for target 'CMakeFiles/counter.dir/all' failed 
mingw32-make.exe[1]: *** [CMakeFiles/counter.dir/all] Error 2 
Makefile:75: recipe for target 'all' failed 
mingw32-make.exe: *** [all] Error 2 

我想實現的是與{project_root}/lib/xstr.c{project_root}/lib/xalloc.c鏈接{project_root}/cat.c

我怎樣才能做到這一點?

+0

嗨,你有沒有關於我的回答任何反饋? – Antonio 2015-04-07 07:37:52

回答

1

您正在使用cmake 太複雜了!您必須在目標中設置依賴關係,而不是源文件。在你的具體情況下,我建議你添加一些中間庫。您可以讓它們爲靜態的,以便鏈接到它們的可執行文件在運行時檢索鏈接庫時沒有問題(可執行文件將集成庫中需要的所有符號和定義)。

您的代碼可以減少如下:

cmake_minimum_required (VERSION 2.8) # <<--2.6 is very outdated 
project (Tools C) 

set(CMAKE_C_FLAGS "-ansi -pedantic -Wall -Werror") 
include_directories("include") 

add_library(xalloc STATIC lib/xalloc.c) 
add_library(xstr STATIC lib/xstr.c) 

add_executable(cat cat.c lib/xfopen.c) 
add_executable(counter counter.c lib/counter.c) 
add_executable(darr dynamic_array.c lib/dynamic_array.c) 
add_executable(linked list.c lib/list.c) 

target_link_libraries(cat xalloc) 
target_link_libraries(counter xstr xalloc) #xstr needs stuff from xalloc 
target_link_libraries(darr xalloc) 
target_link_libraries(linked xalloc) 

注:你確定你需要darrlinked爲可執行文件?它們看起來非常像一個庫...另外,有一個名爲counter.c和一個lib/counter.c的文件似乎不是很安全(dynamic_array和list相同)。

一些有用的鏈接:
http://www.cmake.org/cmake/help/v3.0/command/add_library.html
http://www.cmake.org/cmake/help/v3.0/command/target_link_libraries.html