English 中文(简体)
• 如何在地震建筑系统的名录中使用所有*c文档?
原标题:How to use all *.c files in a directory with the Cmake build system?

我想从目录中找到所有c文档,并将所有文件添加到SRC档案中,以便汇编成 cm。 我如何能够在CMakeList.txt这样做。

普通造册

SPECIFIED_SRC_FILE  = $(foreach d,$(SPECIFIED_SRC_DIRS),$(wildcard $(addprefix $(d)/*,*.c)))

但是,我无法在CMakeList.txt中找到这样的东西。

最佳回答

为此:

将所有来源档案列入目录。

AUX_SOURCE_DIRECTORY(dir VARIABLE) 

Collects the names of all the source files in the specified directory and stores the list in the variable provided. This command is intended to be used by projects that use explicit template instantiation. Template instantiation files can be stored in a "Templates" subdirectory and collected automatically using this command to avoid manually listing all instantiations.

It is tempting to use this command to avoid writing the list of source files for a library or executable target. While this seems to work, there is no way for CMake to generate a build system that knows when a new source file has been added. Normally the generated build system knows when it needs to rerun CMake because the CMakeLists.txt file is modified to add a new source. When the source is just added to the directory without modifying this file, one would have to manually rerun CMake to generate a build system incorporating the new file.

问题回答

如何对待好老的游说?

FILE(GLOB MyCSources *.c)
ADD_EXECUTABLE(MyExecutable ${MyCSources})

<><> 代码>GLOB_RECURSE recursive example

基本上,<代码>*也属于次方向:

cmake_minimum_required(VERSION 3.0)
file(GLOB_RECURSE SOURCES RELATIVE ${CMAKE_SOURCE_DIR} "src/*.c")
add_executable(main ${SOURCES})

And then our sources could be located for example as:

src/main.c
src/d/a.c
src/d/a.h

使用<代码>GLOB_RECURSE在CMake顶级(即*.c>而不是src/*.c”,可能是一种坏的想法,因为它可以检索.c,由CMake本身生成的载于build/的文档。

可操作的例子on Git

<><>只有>> 正确的答案是,而不是>。 详情见 多边。 页: 1 我将在此总结一下,以减少重要性。

  1. The developers have explicitly stated1 that it is wrong to do this. That should give you pause. If you encounter an issue, the developers will likely not be receptive to fixing it for you, but will rather tell you to list your sources.
  2. The aux_source_directory function is outright incorrect since it cannot detect changes in the filesystem. For the same reason, using file(GLOB or file(GLOB_RECURSE without CONFIGURE_DEPENDS is outright incorrect.
  3. CONFIGURE_DEPENDS is not guaranteed to work. (See point 1)
    1. In fact, it was buggy on Windows before Ninja 1.10.2.
    2. It also breaks dry-run workflows since the glob checks run in a parent build and the child (real) build is invoked recursively.
  4. If globbing fails, you will have a difficult time figuring out which extra source file got added or removed.
  5. Globbing, especially recursive globbing, can be slow and gets worse the more files you have. Ext4 s performance is typically acceptable, but NTFS s is bad, especially through Linux drivers. See: https://github.com/alexreinking/cmake-glob-performance/
  6. Globbing is particularly likely to fail when doing git bisects, switching branches, or performing other source control operations that move file timestamps backward.

只是简单地列出你自己的来源。


<>1> 这里,CMake的开发商必须说:

<>说明: 我们不建议利用GLOB收集来源树的原始档案清单。 如果没有CMakeList。 当一个来源被添加或移除时,所产生的建筑系统无法知道何时要求CMake进行再生。 <>CONFIGURE_DEPENDS 旗帜不能对所有发电机进行可靠的工作,或者如果将来添加无法支持的新发电机,则使用这种装置的项目将受到阻碍。 即便是<> CONFIGURE_DEPENDS 可靠地开展工作,每次重建都会有费用进行检查。

是的,你有两个选择。 让我假定,这种双重结构类似。

├── autopilot
            │   ├── _AutoPilot.cpp
            │   ├── _AutoPilot.h
            │   └── action
            │       ├── ActionBase.cpp
            │       ├── ActionBase.h
            │       ├── APcopter
            │       │   ├── APcopter_avoid.cpp
            │       │   ├── APcopter_avoid.h

如果要使用<代码>AUX_SOURCE_DIRECTORY,你必须增加CMakeLists。 每一分局。 然后,你们必须把所有这些分局包括在内并连接起来。 这是一项艰巨的任务。 因此,你可以轻松地做工作。 这就是如何做到这一点。

  file(GLOB autopilot_sources ./**.cpp ./**.c)
  SET( autopilot ${autopilot_sources})  

如果你想使用上述来源代码建立一个图书馆,则指挥是:

  ADD_LIBRARY ( autopilot  ${autopilot_sources})
  TARGET_LINK_LIBRARIES ( autopilot)  

如果你想使用上述来源法建立一个可起诉的档案,则指挥是:

 ADD_EXECUTABLE(autopilot ${autopilot_sources})

你可以使用,描述为@whitequark,但是,由于CMake无法确定何时添加新的档案(这是使用野心的整点)。





相关问题
makefile: how to show line numbers for debugging?

Is there a way to have make display the line number where it declares an error? The -d switch doesn t seem to cut it. Updated: Example output: Reaping winning child 0x08aa8648 PID 9381 /bin/sh: ...

What is the reasoning behind the Makefile whitespace syntax?

I m revisiting Python after Michael Sparks s excellent walk through of Peter Norvig s Python spell checker at the SO DevDay in London. One of the points he highlighted was how clean Python is to look ...

Debugging GNU make

Is there a command line way in make to find out which of the prerequisites of a target is not updated?

How to add an all rule to this Makefile?

I want to just type make all and have the following Makefile do everything it s supposed to do: LEX = lex YACC = yacc CC = gcc calcu: y.tab.o lex.yy.o $(CC) -o calcu y.tab.o lex.yy.o -ly -lfl ...

How to discover number of *logical* cores on Mac OS X?

How can you tell, from the command line, how many cores are on the machine when you re running Mac OS X? On Linux, I use: x=$(awk /^processor/ {++n} END {print n+1} /proc/cpuinfo) It s not ...

Using extern in C doesn t work as expected

I have created two files: tunables.h #ifndef TUNABLES_H #define TUNABLES_H void tunables_load_conservative(); void tunables_load_aggressive(); extern int timer_x; #endif /*TUNABLES_H */ and ...