eclipse for hello world makefile
1. 工程文件分析
使用eclipse新建一个Hello World工程,假设工程名称是hello,此时eclipse在工程目录下新建了一个名为hello的文件夹:
hello/
.cproject
.project
src/
hello.c
先build该工程,然后Clean该工程,清除生成的目标码,这时hello文件夹下只多了eclipse为工程生成的makefile文件,其文件结构为:
hello/
.cproject
.project
Debug/
src/
subdir.mk
makefile
objects.mk
sources.mk
src/
hello.c
2. sources.mk文件
该文件中定义了makefile文件中使用的各种变量,最主要的就是SUBDIRS变量,它指示了工程中所有包含源文件的子目录,其内容如下:
################################################################################
# Automatically-generated file. Do not edit!
################################################################################ O_SRCS :=
C_SRCS :=
S_UPPER_SRCS :=
OBJ_SRCS :=
ASM_SRCS :=
OBJS :=
C_DEPS :=
EXECUTABLES := # Every subdirectory with source files must be described here
SUBDIRS := \
src \
3 objects.mk文件
################################################################################
# Automatically-generated file. Do not edit!
################################################################################ USER_OBJS := LIBS :=
4 src/subdir.mk文件
该文件针对src目录下的所有.c文件修改C_SRCS、OBJS和C_DEPS变量,并定义文件夹下所有.c文件的编译规则。如这里的src/%.o: ../src/%.c,就是针对src文件夹里的每个.c文件,在makefile所在文件夹(即Debug)里生成src文件夹及同名的.o文件。
Eclipse cdt会在所有包含源文件的文件夹下都生成这么一个subdir.mk文件,所有Debug下存放.o的文件夹结构与存放.c文件的完全相同。
################################################################################
# Automatically-generated file. Do not edit!
################################################################################ # Add inputs and outputs from these tool invocations to the build variables
C_SRCS += \
../src/hello.c OBJS += \
./src/hello.o C_DEPS += \
./src/hello.d # Each subdirectory must supply rules for building sources it contributes
src/%.o: ../src/%.c
@echo 'Building file: $<'
@echo 'Invoking: GCC C Compiler'
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '
5 makefile文件
makefile文件include包含了上面介绍的几个文件,下面是makefile文件的内容:
################################################################################
# Automatically-generated file. Do not edit!
################################################################################ -include ../makefile.init RM := rm -rf # All of the sources participating in the build are defined here
-include sources.mk
-include src/subdir.mk
-include subdir.mk
-include objects.mk ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif -include ../makefile.defs # Add inputs and outputs from these tool invocations to the build variables # All Target
all: hello # Tool invocations
hello: $(OBJS) $(USER_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: GCC C Linker'
gcc -o "hello" $(OBJS) $(USER_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' ' # Other Targets
clean:
-$(RM) $(OBJS)$(C_DEPS)$(EXECUTABLES) hello
-@echo ' ' .PHONY: all clean dependents
.SECONDARY: -include ../makefile.targets
这里有必要说明以下MAKECMDGOALS 变量,make 在执行时设置的一个特殊变量,该变量记录了命令行参数指定的目标列表,没有通过参数指定终极目标时此变量为空。该变量仅限于用在特殊场合(比如判断),在 Makefile 中最好不要对它进行重新定义。
而strip 函数可以用来去掉字符串中的空格(包括 [TAB] 等不可显示字符),这些空格的位置可以位于字符串中字符单词的前面,中间以及后面。去掉的结果是用一个空格代替多个原来的多个空格。
所以这个makefile中make目标不为clean,并且C_DEPS包含有效字符时才会-include $(C_DEPS)
在这个工程中,C_DEPS为./src/hello.d,所以make all时要包含该文件,可src下并没有改文件呀?执行make后,发现在src下多了hello.d文件,内容是:
src/hello.d src/hello.o: ../src/hello.c
它是怎么来的呢?内容为何是这样呢?这就需要搞明白src文件夹里subdir.mk中的这条命令了:
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
-fmessage-length=0:默认情况下,GNU工具链编译过程中,控制台输出的一行信息是不换行的,这样,当输出信息过长时(如编译错误时的信息),会导致你无法看到完整的输出信息,加入-fmessage-length=0后,输出信息会根据控制台的宽度自动换行,这样就能看全输出信息了。
-MF FILE:与-M或-MM一起使用,指定依赖关系写到什么文件中,如果没有-MF选项,则默认写到预处理输出中。当与-MD或-MMD一起使用时,-MF指定的文件会覆盖默认依赖输出文件。
-M:输出源文件的依赖关系,其中隐含了-E和-w选项。
-MM:与-M类似,但忽略系统头文件。
-MD:除了未隐含-E选项外,等价于-M -MF file。
-MMD:与-MD类似,但忽略系统头文件。
-MP:为依赖的头文件添加伪目标,目的是当删除头文件后如果makefile没有更新,则提示错误。典型的输出如:
test.o: test.c test.h
test.h:
-MT:改变依赖规则目标,在这个Hello工程makefile中,如果去掉-MT"$(@:%.o=%.d)",那么输出的hello.d文件内容是:
src/hello.d src/hello.o: ../src/hello.c
根据以上分析,我们知道第一次执行make all时并不会包含hello.d文件,因为那时它还不存在,hello.d也正是这次执行才生成的;以后再次执行make all时,会将hello.d内容包括到makefile中,make会根据依赖关系有选择性的进行编译和链接操作。
6 make all
在命令行下面进入Debug目录,执行make all命令,将会看到下面输出:
Building file: ../src/hello.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/hello.d" -MT"src/hello.d" -o "src/hello.o" "../src/hello.c"
Finished building: ../src/hello.c Building target: hello
Invoking: GCC C Linker
gcc -o "hello" ./src/hello.o
Finished building target: hello
其中上面一段是对源文件的编译,规则在src/subdir.mk中定义;后面一段声明目标文件,规则直接在makefile文件中定义。
7 其他
除了上面分析的内容,makefile文件还有下面几项:
-include ../makefile.init
-include ../makefile.defs
.PHONY: dependents
-include ../makefile.targets
eclipse for hello world makefile的更多相关文章
- import c++ project to eclipse cdt with exiting makefile
		
Step 2: You are now ready to build your project. To build your project, select Project > Build Pr ...
 - 【Eclipse】编译使用Makefile的C工程
		
创建MakeFile project新建src文件夹,将文件复制到里面.右击makefile,make targets->create->名称填上allmake targets->b ...
 - 将含有makefile文件的源码加入Eclipse工程
		
转载自https://www.linuxidc.com/Linux/2011-02/32763.htm 很多软件在开发或者分析时需要一个像样的IDE,Eclipse是其中很优秀的一个,至少个人感觉很好 ...
 - eclipse 利用已有c++代码建工程,并编译执行
		
如果你想建一个带Makefile的c++ 工程 1. 新建一个C++空工程,工程类型是makefile project,选择Linux GCC: 2. 将源码连同makefile文件一同作为一个文件系 ...
 - (转)用Eclipse编译你的ROS程序
		
原地址: http://blog.csdn.net/sujun3304/article/details/18572017 好了,理解了系统各个组件的含义后,还是直接进入程序真刀真枪的从实践中学习吧! ...
 - 在eclipse中的交叉编译
		
1.硬件是Arm 9的板子,运行的系统是Ubuntu 12.05 2.电脑虚拟机上安装的系统是Ubuntu 10.04,程序的开发都是在虚拟机上完成,开发IDE是eclipse 3.eclipse 默 ...
 - How to Install Eclipse C/C++ Development Tool--转
		
http://www3.ntu.edu.sg/home/ehchua/programming/howto/EclipseCpp_HowTo.html Eclipse 4.3 (Kepler) for ...
 - Linux下EclipseCDT工程和TFS的持续集成CI实践
		
在Linux下使用TFS自动构建,需要自动执行连接tfs服务器的操作,命令行文件包TEE-CLC-10.1.0.2011121402.zip,下载地址:http://www.microsoft.com ...
 - Android Studio NDK 学习之接受Java传入的字符串
		
本博客是基于Android Studio 1.3 preview版本,且默认你已经安装了Android SDK, Android NDK. 用Android Studio新建一个工程叫Prompt,其 ...
 
随机推荐
- Inheritance, Association, Aggregation, and Composition 类的继承,关联,聚合和组合的区别
			
在C++中,类与类之间的关系大概有四种,分别为继承,关联,聚合,和组合.其中继承我们大家应该都比较熟悉,因为是C++的三大特性继承Inheritance,封装Encapsulation,和多态Poly ...
 - ArcEngine开发:IElement.Geometry 值不在预期范围内 + 元素绘制代码
			
IElement pEle = pLineEle as IElement; pEle.Geometry = pLn; pLn为一个ILine对象,想当然的以为它是IGeometry对象,可以赋值,结果 ...
 - shopnc nginx优化配置文件
			
user www; worker_processes 2; error_log /var/log/nginx/error.log error; #error_log logs/error.log no ...
 - Laravel Eloquent 判断取出的结果集是否为空
			
在使用Laravel Eloquent模型时,我们可能要判断取出的结果集是否为空,但我们发现直接使用is_null或empty是无法判段它结果集是否为空的. var_dump之后我们很容易发现,即使取 ...
 - Linux 计划任务 Crontab 笔记与总结(4)crontab 的日志
			
/var/log/cron 文件保存 cron 的任务执行记录 cd /var/log/ ls -l cron* 会发现每天都会有 cron 日志的变化 使用 tail -f cron 能够查看今天的 ...
 - Web 在线文件管理器学习笔记与总结(9)下载文件
			
① 普通形式的文件可以使用超链接形式下载 <a href = '下载文件名'>点击下载</a> ② 如果下载图片.html 等类型的文件,使用header() 函数发送网页头信 ...
 - UITextField里面的 placeholder颜色和字体
			
// placeholder 颜色 [field setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor& ...
 - Ogre初入手:最简单的ogre程序骨架
			
本文内容主要参考于页面 http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Ogre+Wiki+Tutorial+Framework Ogre是一个非 ...
 - Linguistic corpora   种子语料库-待分析对象-分析与更新语料库
			
Computational Linguistics http://matplotlib.org/ https://github.com/matplotlib/matplotlib/blob/maste ...
 - 从cookie的setDomain方法可以得知localhost不是域名
			
http://www.baidu.com 其中baidu.com是一个域名.那么http://localhost 中的localhost是不是域名呢?我百度过,发现有人说这是域名.于是我在自己的web ...