如何指定依赖于其他文件的文件

时间:2015-04-01 19:57:19

标签: cmake

我对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)

我在我的根目录中保留包含main()函数的c文件。我将其他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}/cat.c{project_root}/lib/xstr.c{project_root}/lib/xalloc.c等联系起来。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分: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