build

什么是build ?

stackoverflow上一位哥们这么说的:

Building means many things to many people, but in general it means starting with source files produced by developers and ending with things like installation packages that are ready for deployment.

"The build" can contain many things:

  • Compilation of source files (for languages/environments that support a separate/explicit compilation step)
  • Linking of object code (for languages/environments that support a separate/explicit linking step)
  • Production of distribution packages, also called "installers"
  • Generation of documentation that is embedded within the source code files, e.g. Doxygen, Javadoc
  • Execution of automated tests like unit tests, static analysis tests, and performance tests
  • Generation of reports that tell the development team how many warnings and errors occurred during the build
  • Deployment of distribution packages. For example, the build could automatically deploy/publish a new version of a web application (assuming that the build is successful).

"The build" can be done "by hand" or it can be automated, or some hybrid of the two. A manual build is a build that requires build commands like compilers to be executed one by one. An automated build packages together all of the individual build tools into a large build program that can be (ideally) run in a single step.


build 工具

Ant

Apache Tutorial


++++++++++++++++++++++++++

eclipse中的java build

Build Classpath


The build classpath specifies which Java source files and resource files in a project are considered by the Java builder and specifies how to find types outside of the project. The Java builder compiles the Java source files into the output folder and also copies the resources into it. The build classpath is specified for each project. In the project properties, it is referred to as the Java Build Path。

Java Builder

The Java builder builds Java programs using its own compiler (the Eclipse Compiler for Java) that implements the Java Language Specification.  The Java builder can build programs incrementally as individual Java files are saved. Note that the Eclipse Compiler for Java can also be invoked using Ant as described in the Using the ant javac adapter section.



Source tab
Source folders are top-level folders in the project hierarchy. They are the root of packages containing .java files. The compiler will translate the contained files to .class files that will be written to the output folder.

Projects tab

In the Required projects on the build path list, you can add project dependencies by selecting other workbench projects to add to the build path for this new project.

Adding a required project indirectly adds all its classpath entries marked as 'exported'. Setting a classpath entry as exported is done in the Order and Export tab.

The projects selected here are automatically added to the referenced projects list. The referenced project list is used to determine the build order. A project is always build after all its referenced projects are built.

Libraries tab

On this page, you can add libraries to the build path.

By default, the library list contains an entry representing the Java runtime library. 

Order and Export tab

In the   Build class path order   list, you can click the   Up   and   Down   buttons to move the selected path entry up or down in the build path order for this new project.
Checked list entries are marked as exported. Exported entries are visible to projects that require the project. Use the  Select All  and  Deselect All  to change the checked state of all entries. Source folders are always exported, and can not be deselected.





<think>我们正在讨论Makefile.build文件的作用和编写方法。根据提供的引用资料,我们可以总结出以下几点: 1. 引用[1]描述了构建过程:主Makefile调用子目录的Makefile,将源码编译成build-in.o,然后汇总链接成可执行程序。 2. 引用[2]介绍了Makefile的基本功能:定义编译规则、管理依赖关系、自动化任务。 3. 引用[3]展示了一个带子目录的工程中Makefile的写法,包括子目录的编译和依赖文件(.d文件)的处理。 4. 引用[4]说明了在多个目录中编译时,需要在configure.ac中声明每个Makefile。 5. 引用[5]给出了生成可执行文件的规则示例,包括链接动态库。 结合这些信息,我们可以推断Makefile.build文件通常用于构建过程中的某个特定步骤,尤其是在多级目录的复杂项目中。它可能是一个被主Makefile调用的子Makefile,用于编译某个子目录的代码。 **Makefile.build的作用:** - 通常用于定义如何编译某个子目录中的源文件。 - 将子目录中的源文件编译成目标文件(通常是build-in.o),然后由上级Makefile将这些目标文件链接成最终的可执行文件或库。 - 管理子目录内的依赖关系,确保在头文件或源文件改变时重新编译。 **编写Makefile.build的方法:** 1. **定义目标:** 通常目标是将当前目录下的源文件编译成一个build-in.o文件。 2. **指定源文件:** 使用变量(如obj-y)列出需要编译的源文件(不带后缀),例如: ```makefile obj-y := file1.o file2.o ``` 这表示要编译file1.c和file2.c(假设是C语言)。 3. **指定编译选项:** 可以通过EXTRA_CFLAGS等变量添加编译选项,例如: ```makefile EXTRA_CFLAGS := -I../include ``` 4. **处理子目录:** 如果当前目录下还有子目录需要编译,可以这样处理: ```makefile subdir-y := dir1 dir2 ``` 然后在规则中进入这些子目录编译: ```makefile $(obj)/build-in.o: $(subdir-y) ``` 5. **依赖文件(.d文件):** 为了正确处理头文件依赖,可以生成.d文件(依赖文件),并在编译时包含它们。例如: ```makefile dep_files := $(foreach f,$(obj-y),$(dir $(f)).$(notdir $(f)).d) -include $(dep_files) ``` 6. **清理规则:** 实现clean目标以删除生成的文件。 **示例Makefile.build:** 下面是一个简化的示例,假设在子目录`src`中: ```makefile # 定义要编译的源文件(不带后缀,自动加上.o) obj-y := main.o helper.o # 如果有子目录,比如src/utils subdir-y := utils # 编译选项 EXTRA_CFLAGS := -I../include # 默认目标:build-in.o __build: $(subdir-y) build-in.o # 进入子目录编译 $(subdir-y): $(Q)$(MAKE) -C $@ -f $(TOP_DIR)/Makefile.build # 生成build-in.o build-in.o: $(obj-y) $(LD) -r -o $@ $^ # 从.c文件生成.o文件 %.o: %.c $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $< -MD -MF .$@.d # 生成依赖文件 # 包含依赖文件 dep_files := $(foreach f,$(obj-y),.$(f).d) -include $(dep_files) # 清理 clean: rm -f *.o .*.d for dir in $(subdir-y); do \ $(MAKE) -C $$dir -f $(TOP_DIR)/Makefile.build clean; \ done ``` **注意:** - 这个示例中使用了变量`$(Q)`(通常用于控制是否输出命令)、`$(MAKE)`(make程序)、`$(CC)`(C编译器)、`$(LD)`(链接器)等,这些通常在顶层Makefile中定义。 - `-MD -MF .$@.d` 选项用于生成依赖文件(.d文件),这些文件记录了源文件包含的头文件,以便在头文件改变时重新编译。 - 顶层Makefile会调用这个Makefile.build,例如: ```makefile all: $(MAKE) -C src -f Makefile.build ``` 通过以上方式,Makefile.build实现了对子目录的递归构建,将子目录的代码编译成build-in.o,最终在顶层链接成可执行程序。 §§相关问题§§ 1. 在大型项目中,如何组织多个Makefile.build文件? 2. Makefile.build中如何处理动态库和静态库的生成? 3. Makefile.build与CMake等构建工具有什么异同? 4. 如何调试Makefile.build中的变量和规则? 5. 在Makefile.build中如何高效地管理头文件依赖?</think>### Makefile.build 文件的作用和编写方法 #### 一、Makefile.build 的核心作用 1. **递归构建管理** Makefile.build 通常作为**子目录构建控制器**,用于实现递归编译。当顶层 Makefile 调用子目录时,通过 `-f Makefile.build` 指定统一的构建规则,避免每个子目录重复编写复杂规则[^1][^3]。 2. **依赖关系自动化** 自动生成 `.d` 依赖文件(如 `main.o.d`),记录源文件与头文件的依赖关系。当头文件修改时触发重新编译,实现**增量编译优化**[^3]。 3. **目标文件聚合** 将子目录中的源文件编译为 `build-in.o`(或类似名称的聚合目标文件),供顶层 Makefile 统一链接。例如: ```makefile # 子目录 Makefile.build 示例 obj-y := file1.o file2.o # 声明目标文件 build-in.o: $(obj-y) $(LD) -r -o $@ $^ # 合并为目标文件 ``` 4. **编译规则标准化** 通过预定义变量(如 `EXTRA_CFLAGS`)统一传递编译选项,确保所有子目录使用相同的编译标准[^3]。 #### 二、编写规范(结合 Linux 内核风格) ```makefile # 示例:display 子目录的 Makefile.build EXTRA_CFLAGS := -I../include # 添加头文件路径 # 1. 声明目标文件 obj-y += disp_manager.o obj-y += framebuffer.o # 2. 定义默认目标 __build: build-in.o # 3. 聚合目标规则 build-in.o: $(obj-y) $(LD) -r -o $@ $^ # 4. 清理规则 clean: rm -f *.o .*.d ``` #### 三、关键编写技巧 1. **依赖文件处理** 通过编译器自动生成 `.d` 文件实现精准增量编译: ```makefile %.o: %.c $(CC) $(CFLAGS) -c $< -o $@ -MD -MF .$@.d # 生成依赖文件 -include .*.d # 包含所有依赖文件 ``` [^3] 2. **递归调用子目录** 支持多级子目录构建: ```makefile subdir-y := utils/ network/ # 声明子目录 $(foreach dir,$(subdir-y),$(MAKE) -C $(dir) -f $(TOP)/Makefile.build) ``` 3. **变量传递** 顶层 Makefile 通过 `export` 传递全局变量: ```makefilefile # 顶层 Makefile export CROSS_COMPILE := arm-linux- export CC := $(CROSS_COMPILE)gcc ``` 4. **配置集成** 在 `configure.ac` 中声明所有 Makefile.build 路径: ```m4 AC_CONFIG_FILES([Makefile src/Makefile.build lib/Makefile.build]) ``` [^4] #### 四、典型构建流程 1. 顶层 Makefile 调用子目录: ```makefile all: $(MAKE) -C src -f ../Makefile.build ``` 2. 子目录生成 `build-in.o` 3. 顶层链接最终目标: ```makefile app: $(SUBDIRS_BUILD_IN) $(CC) -o $@ $^ -lhello # 链接库文件[^5] ``` #### 五、常见问题解决 - **新增文件**:在 `obj-y` 中添加目标文件名(如 `obj-y += newfile.o`)[^1] - **头文件更新**:确保 `.d` 文件被正确包含,触发重新编译[^3] - **跨平台编译**:通过 `export` 传递工具链变量(如 `CROSS_COMPILE`) > 通过 Makefile.build 实现的递归构建系统,在 Linux 内核和 U-Boot 等大型项目中广泛应用,可管理数万个源文件的高效编译[^1][^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值