博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一步一步写一个简单通用的makefile(一)
阅读量:6950 次
发布时间:2019-06-27

本文共 5289 字,大约阅读时间需要 17 分钟。

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

 

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

hellomake .c:

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

 

hellofunc.c:

#include "hellofunc.h"#include
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.cgcc -o hellomake hellomake.o hellofunc.o -lm

gcc -Wall -o hellofunc.o hellofunc.c

gcc -Wall -o hellomake.o hellomake.c

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

#Hellomake#Magnum, 2014-10-19# 指令编译器和选项  CC=gcc  CFLAGS=-Wall LIBS=-lm  # 目标文件  TARGET=hellomakeSRCS = 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.cgcc   -Wall  -o hellomake.o -c hellofunc.cgcc   -o hellomake hellofunc.o hellomake.o -lmhellomake.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 0 has invalid symbol index 10/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 11/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 10/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 12/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 12/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 12/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 2/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 2/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 11/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 12/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 12/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 12/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 12/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 12/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 12/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 12/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 12/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 12/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 19/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 1 exit statusmake: *** [hellomake] Error 1

错自第二句:

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

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

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

执行make,编译OK。

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

#Hellomake#Magnum, 2014-10-19# 指令编译器和选项  CC=gcc  CFLAGS=-Wall LIBS=-lm  # 目标文件  TARGET=hellomakeSRCS = $(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.oclean:      rm -rf $(TARGET) $(OBJS)  %.o:%.c      $(CC) $(CFLAGS) -o $@ -c $<   #$

 

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

转载地址:http://gocil.baihongyu.com/

你可能感兴趣的文章
PHP导出超大的CSV格式的Excel表方案
查看>>
Mac 环境下如何生成Git shh key
查看>>
jenkins 使用磁盘检查插件 disk check plugin
查看>>
使用 Ruby 拓展 Vim
查看>>
centos7下安装LNMP(nginx+PHP7.1.9+mysql5.7)
查看>>
NodeAPI学习之Buffer
查看>>
深入java单例模式
查看>>
create-react-app
查看>>
20170812-XSS跨站脚本攻击
查看>>
Let’s Build |> 使用Elixir,Phoenix和React打造克隆版的Slack(part 1)
查看>>
如何让 StackNaivgator 实现越级回跳
查看>>
工具简述
查看>>
Hbase 集群搭建
查看>>
分布式文件服务器dfs
查看>>
正则表达式
查看>>
关于直播视频格式和浏览器兼容性历史的来龙去脉
查看>>
是的,InfoQ正在招聘技术编辑!跟对的人,一起做喜欢的事!
查看>>
vue2+vue-cli,dis文件加载出错解决方案
查看>>
立下“去O”Flag的AWS,悄悄修炼了哪些内功?
查看>>
关于团队建设,穆帅能教我们什么?
查看>>