经常会用写一些小的程序有的是作为测试,但是每次都需要写一些简单的GCC 命令,有的时候移植一些项目中的部分代码到小程序里面进行测试,这个时候GCC 命令并不好些,如果写啦一个比较常用的makefile的模板,然后把文件添加进来,简单的修改一下makefile即可以完成测试任务何乐而不为。

源代码有三个文件,三个文件在同一个目录下面/hellomake
hellomake .c:

#include "hellofunc.h"
#include<stdio.h>
#include<math.h>
int main() {
// call a function in another file
myPrintHelloMake();
double value =;
printf("Value:%f\n",log(value));
return();
}

hellofunc.c:

#include "hellofunc.h"
#include<stdio.h>
void myPrintHelloMake(void) { printf("Hello makefiles!\n");
return;
}

hellofunc.h

/*
example include file
*/ void myPrintHelloMake(void);

编译,执行gcc 命令如下:

gcc -Wall -o hellomake hellomake.c hellofunc.c -lm

编译生成可执行文件hellomake.

执行命令:"./hellomake",结果如下:

Hello makefiles!
Value:2.708050

gcc 的命令执行顺序应该编译源文件生成目标文件,然后链接目标文件生成可执行文件,执行命令如下:

gcc -Wall -c hellomake.c hellofunc.c
gcc -o hellomake hellomake.o hellofunc.o -lm

gcc -Wall -o hellofunc.o hellofunc.c

gcc -Wall -o hellomake.o hellomake.c

在当前目录下添加makefile文件:

#Hellomake
#Magnum, --
# 指令编译器和选项
CC=gcc
CFLAGS=-Wall
LIBS=-lm # 目标文件
TARGET=hellomake
SRCS = hellofunc.c \
hellomake.c # 依赖目标
OBJS =$(SRCS:.c=.o) $(TARGET):$(OBJS)
#@echo TARGET:$(OBJS)
# @echo OBJECTS:$^
$(CC) -o $@ $^ $(LIBS) clean:
rm -rf $(TARGET) $(OBJS) $(OBJS):$(SRCS)
$(CC) $(CFLAGS) -o $@ -c $<

执行make,报错:

gcc   -Wall  -o hellofunc.o -c hellofunc.c
gcc -Wall -o hellomake.o -c hellofunc.c
gcc -o hellomake hellofunc.o hellomake.o -lm
hellomake.o: In function `myPrintHelloMake':
hellofunc.c:(.text+0x0): multiple definition of `myPrintHelloMake'
hellofunc.o:hellofunc.c:(.text+0x0): first defined here
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation has invalid symbol index
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned exit status
make: *** [hellomake] Error

错自第二句:

gcc   -Wall  -o hellomake.o -c hellofunc.c

使用$(OBJS):$(SRCS)并不会自动推进,修改makefile文件:

#Hellomake
#Magnum, --
# 指令编译器和选项
CC=gcc
CFLAGS=-Wall
LIBS=-lm # 目标文件
TARGET=hellomake
SRCS = hellofunc.c \
hellomake.c # 依赖目标
OBJS =$(SRCS:.c=.o) $(TARGET):$(OBJS)
# @echo TARGET:$(OBJS)
# @echo OBJECTS:$^
$(CC) -o $@ $^ $(LIBS) #$@指目标文件,这里是hellomake,$^指所有依赖文件,这里指hellofunc.o,hellomake.o clean:
rm -rf $(TARGET) $(OBJS) %.o:%.c
$(CC) $(CFLAGS) -o $@ -c $< #$<指第一个依赖文件此处以此为hellofunc.c,hellomake.c

执行make,编译OK。

如果有更多的源文件,一个个加很麻烦,可以用wildcard 来解决这个问题,进一步版本的makefile如下:

#Hellomake
#Magnum, --
# 指令编译器和选项
CC=gcc
CFLAGS=-Wall
LIBS=-lm # 目标文件
TARGET=hellomake
SRCS = $(wildcard *.c) #当前文件夹下面的所有.c文件
#SRCS = hellofunc.c \
# hellomake.c # 依赖目标
OBJS =$(SRCS:.c=.o) # $(TARGET):$(OBJS)
# @echo TARGET:$(OBJS)
# @echo OBJECTS:$^
$(CC) -o $@ $^ $(LIBS) #$@指目标文件,这里是hellomake,$^指所有依赖文件,这里指hellofunc.o,hellomake.o clean:
rm -rf $(TARGET) $(OBJS) %.o:%.c
$(CC) $(CFLAGS) -o $@ -c $< #$<指第一个依赖文件此处以此为hellofunc.c,hellomake.c

这里到这结束,下一篇是跨多个文件夹的makefile 如何编写。

一步一步写一个简单通用的makefile(一)的更多相关文章

  1. 一步一步写一个简单通用的makefile(三)

    上一篇一步一步写一个简单通用的makefile(二) 里面的makefile 实现对通用的代码进行编译,这一章我将会对上一次的makefile 进行进一步的优化. 优化后的makefile: #Hel ...

  2. [置顶] 自己写一个简单通用的Makefile

    转自:http://blog.csdn.net/u011913612/article/details/52102241 一.makefile的作用 Makefile是用于自动编译和链接的,一个工程有很 ...

  3. 一步一步写一个简单通用的makefile(四)--写一个通用的makefile编译android可执行文件

    通常要把我们自己的的代码编译成在android里面编译的可执行文件,我们通常是建一个文件夹 . ├── Android.mk ├── Application.mk ├── convolve.cl ├─ ...

  4. 一步一步写一个简单通用的makefile(二)

    这一篇源代码沿用上一篇的源代码hellomake.c hellofunc.c hellofunc.h makefile 但是代码内容和结构有变化,如下: . ├── include │   └── h ...

  5. (原创)如何使用boost.asio写一个简单的通信程序(一)

    boost.asio相信很多人听说过,作为一个跨平台的通信库,它的性能是很出色的,然而它却谈不上好用,里面有很多地方稍不注意就会出错,要正确的用好asio还是需要花一番精力去学习和实践的,本文将通过介 ...

  6. linux设备驱动第三篇:如何写一个简单的字符设备驱动?

    在linux设备驱动第一篇:设备驱动程序简介中简单介绍了字符驱动,本篇简单介绍如何写一个简单的字符设备驱动.本篇借鉴LDD中的源码,实现一个与硬件设备无关的字符设备驱动,仅仅操作从内核中分配的一些内存 ...

  7. 用node.js从零开始去写一个简单的爬虫

    如果你不会Python语言,正好又是一个node.js小白,看完这篇文章之后,一定会觉得受益匪浅,感受到自己又新get到了一门技能,如何用node.js从零开始去写一个简单的爬虫,十分钟时间就能搞定, ...

  8. linux设备驱动第三篇:写一个简单的字符设备驱动

          在linux设备驱动第一篇:设备驱动程序简介中简单介绍了字符驱动,本篇简单介绍如何写一个简单的字符设备驱动.本篇借鉴LDD中的源码,实现一个与硬件设备无关的字符设备驱动,仅仅操作从内核中分 ...

  9. 用C写一个简单的推箱子游戏(一)

    我现在在读大二,我们有一门课程叫<操作系统>,课程考查要求我们可以写一段程序或者写Windows.iOS.Mac的发展历程.后面我结合网上的资料参考,就想用自己之前简单学过的C写一关的推箱 ...

随机推荐

  1. gitlab的安装以及汉化

    gitlab的安装 首先在网上下载好任意版本gitlab的rpm包 推荐下面的地址: https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gi ...

  2. C#控件命名规范

    文档名称: C#控件命名规范 撰写作者: codefly 版本编号: V1.1 C#控件命名规范 一.Data Control 类型 前缀 示例 AccessDataSource ads adsPub ...

  3. mini2440 MJPG_STREAMER 产生问题

    usb摄像头芯片是中芯微的zc0301pl, http://www.vimicro.com.cn/product/pdf/ZC301PL-1107-V10-EN.pdf [root@FriendlyA ...

  4. linux运维工程师,必须掌握以下几个工具

    本人是linux运维工程师,对这方面有点心得,现在我说说要掌握哪方面的工具吧说到工具,在行外可以说是技能,在行内我们一般称为工具,就是运维必须要掌握的工具.我就大概列出这几方面,这样入门就基本没问题了 ...

  5. Null Pointer --设计模式

    在Joshua Bloch很有名的一本书<Effective in java>中建议不要在代码中返回空的collection/map/array,就像下面的代码一样: public Lis ...

  6. How to say all the keyboard symbols in English and Chinese

    How to say all the keyboard symbols in English Symbol English 中文 ~ tilde 波浪号 ` grave accent, backquo ...

  7. SimpleXML解析xml文件

    SimpleXML 扩展提供了一种获取 XML 元素的名称和文本的简单方式. 与 DOM 或 Expat 解析器相比,SimpleXML 仅仅用几行代码就可以从 XML 元素中读取文本数据. Simp ...

  8. wdcp日志

    apache或nginx都有开关默认日志,一个是正常访问日志,一个是错误的日志,目录在 /www/wdlinux/nginx-1.0.15/logs /www/wdlinux/httpd-2.2.22 ...

  9. Java RMI(远程方法调用)开发

    参考 https://docs.oracle.com/javase/7/docs/platform/rmi/spec/rmi-arch2.html http://www.cnblogs.com/wxi ...

  10. VS2015安装开发ios android

    前几天很火,装了一下,结果是不是太满意,装了VS2015只是多了一个android和ios的模版,最终还是要装xamarin ,最后装了个xamarin ,然后破解 破解地址:http://www.c ...