一步一步写一个简单通用的makefile(一)
经常会用写一些小的程序有的是作为测试,但是每次都需要写一些简单的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(一)的更多相关文章
- 一步一步写一个简单通用的makefile(三)
上一篇一步一步写一个简单通用的makefile(二) 里面的makefile 实现对通用的代码进行编译,这一章我将会对上一次的makefile 进行进一步的优化. 优化后的makefile: #Hel ...
- [置顶]
自己写一个简单通用的Makefile
转自:http://blog.csdn.net/u011913612/article/details/52102241 一.makefile的作用 Makefile是用于自动编译和链接的,一个工程有很 ...
- 一步一步写一个简单通用的makefile(四)--写一个通用的makefile编译android可执行文件
通常要把我们自己的的代码编译成在android里面编译的可执行文件,我们通常是建一个文件夹 . ├── Android.mk ├── Application.mk ├── convolve.cl ├─ ...
- 一步一步写一个简单通用的makefile(二)
这一篇源代码沿用上一篇的源代码hellomake.c hellofunc.c hellofunc.h makefile 但是代码内容和结构有变化,如下: . ├── include │ └── h ...
- (原创)如何使用boost.asio写一个简单的通信程序(一)
boost.asio相信很多人听说过,作为一个跨平台的通信库,它的性能是很出色的,然而它却谈不上好用,里面有很多地方稍不注意就会出错,要正确的用好asio还是需要花一番精力去学习和实践的,本文将通过介 ...
- linux设备驱动第三篇:如何写一个简单的字符设备驱动?
在linux设备驱动第一篇:设备驱动程序简介中简单介绍了字符驱动,本篇简单介绍如何写一个简单的字符设备驱动.本篇借鉴LDD中的源码,实现一个与硬件设备无关的字符设备驱动,仅仅操作从内核中分配的一些内存 ...
- 用node.js从零开始去写一个简单的爬虫
如果你不会Python语言,正好又是一个node.js小白,看完这篇文章之后,一定会觉得受益匪浅,感受到自己又新get到了一门技能,如何用node.js从零开始去写一个简单的爬虫,十分钟时间就能搞定, ...
- linux设备驱动第三篇:写一个简单的字符设备驱动
在linux设备驱动第一篇:设备驱动程序简介中简单介绍了字符驱动,本篇简单介绍如何写一个简单的字符设备驱动.本篇借鉴LDD中的源码,实现一个与硬件设备无关的字符设备驱动,仅仅操作从内核中分 ...
- 用C写一个简单的推箱子游戏(一)
我现在在读大二,我们有一门课程叫<操作系统>,课程考查要求我们可以写一段程序或者写Windows.iOS.Mac的发展历程.后面我结合网上的资料参考,就想用自己之前简单学过的C写一关的推箱 ...
随机推荐
- apache config directive – order, allow, deny
在对apache进行配置的时候,常看到oerder, allow, deny.现在就简单回顾一下其用法. 对于每个对于资源的请求,服务器可以配置是否允许这个请求通过.在配置当中,使用的是允许与不允许的 ...
- Ubuntu下Java环境配置
Oracle Java安装: 通过以下命令进行安装: sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt ...
- mysql 查找包含特定名字的表
SELECT distinct TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME LIKE '%medias%'
- 系统监控的一些工具w , vmstat
w 命令:--w 查看的是系统整体上的负载 # w 15:23:46 up 3:34, 2 users, load average: 0.03, 0.05, 0.00 USER TTY FROM LO ...
- IE8+等兼容、360调用webkit内核小记
首先是处理IE8.9等的兼容问题,注意以下几点: 1,尽可能严格要求自己使用w3c推荐的方式编写html/css 2,在html页面顶部添加<!DOCHTML html>,不清楚请查看参考 ...
- java 面向对象——进度1
面向对象:1,面向对象和面向过程思想. 面向对象强调的是对象实例. 面向过程强调的是动作. 对象将动作进行封装. 在问题领域中,我们先去找的都是涉及的对象, 然后 ...
- OS概论2
实时系统 实时即表示及时,实时计算可以定义为这样一类计算:系统的正确性,不仅由计算的逻辑结果来确定,而且还取决于产生结果的时间.事实上,实时系统最主要的特征,是将时间作为关键参数,它必须对所接收到的某 ...
- LFS实践
用了三天,编译了两次LFS,把LFS的基本流程和原理都弄清了.用的是LFS 6.3,使用的教程是LFS速成手册(6.3) ,感觉很不错,如果按照它的做法,一步一步来,基本都能编译成功而且没什么错误.不 ...
- DataGridView出现大红叉--在使用多线程访问数据源时
datagridview 的数据源操作在一个方面里面处理 不要多个地方处理 并且处理的时候要加锁 红叉 应该是多线程操作出现的. try catch 只是起到 捕获异常的功能,但是一旦出现了这种错误 ...
- C#数据类型-string
string是各种编程语言中最基础的数据类型,长期以来受尽其它类的压迫,经常被肢解(Substring.Split).蹂躏(Join)... 而现在C#数据类型string要“翻身闹革命”了,它几乎无 ...