当前位置:首页 » 编程软件 » makefile编译实例

makefile编译实例

发布时间: 2022-07-13 06:45:50

‘壹’ 如何使用Makefile自动编译iPhone程序

makefile里面所写的内容其实就是你要编译的命令,那么,什么是编译命令呢?
假写你已经写好一个程序代码,并将之存在一个.c文件中,如:hello.c,在终端上你可以这样做!在终端上输入gcc -o hello hello.c
然后回车,看一看有没有什么反映,如果没有打出很多英文的话,恭喜你!你完美地完成了第一步!然后,在终端中输入./hello 看看是不是有什么输出了?
现在来解释一下编译命令:上面的命令的意思就是,使用gcc编译器编译hello.c源代码,生成的文件名称叫做hello.最后,要看程序运行结果,就要运行生成的程序也就是“./hello”了,“./”的意思就是在当前的目录下运行。
而makefile中内容的就是上面的编译命令,如:在makefile文件中写入
Hello:hello.c
gcc -o Hello hello.c
保存文件之后直接在终端中输入make,就完成编译了!makefile存在的意义只是让编译更加方便,也就说,可以把所以的编译都写在一个makefile文件中,然后在终端中输入make就可以完成makefile文件里的命令!
建议还是先将c语言入门,然后再学使用makefile编译程序吧!因为刚开始的时候不用编译很多文件,如果一个文件要编写一个makefile文件的话,那岂不是很繁?

‘贰’ 如何写Makefile编译汇编和C文件

有些场景下编译的程序是不能依赖OS和标准的C库的,并且需要C和汇编混合编译,如bootloader程序,就需要制定参数-nostdlib,这样的Makefile如下所示:
all:
arm-linux-gcc -O2 -Wall -nostdlib -march=armv4 -Wl,-T,ipl.lds uart.c ipl.c nfc.c nand.c sha1.c arm.s -o ipl.exe
arm-linux-obj -Obinary ipl.exe ipl.bin
clean:
rm -rf ipl.exe ipl.bin

‘叁’ 大家来帮我看看怎样写一个这样的Makefile文件,通过Makefile文件编译这个多文件的C语言程序

以下是一个最简单的多文件+makefile的形式

编译采用gcc 你可以修改成你的编译器


三个文件 main.c func.c makefile

main.c

#include<stdio.h>
externvoidfunc();//这个应该放在头文件中的比如func.h简单起见就直接声明了

intmain()
{
func();
}

func.c

#include<stdio.h>

voidfunc()
{
printf("helloworld ");
}

makefile

.PHONY:allmain.ofunc.o

all:main.ofunc.o
@gccmain.ofunc.o-oout
main.o:main.c
@gcc-cmain.c-omain.o
func.o:func.c
@gcc-cfunc.c-ofunc.o

所有的都是最简单的

‘肆’ 如何写一个简单的makefile

一:makefile 雏形:
#makefile的撰写是基于规则的,当然这个规则也是很简单的,就是:
#target : prerequisites
command//任意的shell 命令
实例如下:
makefile:
helloworld : main.o print.o #helloword 就是我们要生成的目标
# main.o print.o是生成此目标的先决条件
gcc -o helloworld main.o print.o#shell命令,最前面的一定是一个tab键
mian.o : mian.c print.h
gcc -c main.c
print.o : print.c print.h
gcc -c print.c
clean :
rm helloworld main.o print.o
OK,一个简单的makefile制作完毕,现成我们输入 make,自动调用Gcc编译了,
输入 make clean就会删除 hellowworld mian.o print.o
二:小步改进:
在上面的例子中我们可以发现 main.o print.o 被定义了多处,
我们是不是可以向C语言中定义一个宏一样定义它呢?当然可以:
makefile:
objects = main.o print.o #应该叫变量的声明更合适
helloworld : $(objects) //声明了变量以后使用就要$()了
gcc -o helloworld$(objects)
mian.o : mian.c print.h
gcc -c main.c
print.o : print.c print.h
gcc -c print.c
clean :
rm helloworld $(objects)
修改完毕,这样使用了变量的话在很多文件的工程中就能体现出方便性了。
三:再进一步:
再看一下,为没一个*.o文件都写一句gcc -c main.c是不是显得多余了,
能不能把它干掉?而且 main.c 和print.c都需要print.h,为每一个都写上是
不是多余了,能不能再改进?
能,当然能了:
makefile:
objects = main.o print.o
helloworld : $(objects)
gcc -o helloworld$(objects)
$(objects) : print.h # 都依赖print.h
mian.o : mian.c #干掉了gcc -c main.c 让Gun make自动推导了。
print.o : print.c
clean :
rm helloworld $(objects)

‘伍’ makefile 怎么编译 c文件

1.单个.c文件
kernel配置文件中定义
CONFIG_RUNYEE_CAMVIB=m
注意上面的m,表示作为一个模块进行编译,最后在MAKEFILE中需要用到的编译开关。
然后再相应的源码目录中的MAKEFILE中添加如下语句:
obj-$(CONFIG_RUNYEE_CAMVIB) := camvib.o

‘陆’ 如何用makefile进行编译

假设有下面几个c++文件:wherewhen.h wherewhen.c countdown.h countdown.c 包含了math.h, 需要连接库文件 main.c 主函数, main.c 包含了两个头文件 wherewhen.h and countdown.h 1、第一种编译方法:g++ -Wall -g wherewhen.c countdown.c main.c -lm -o myprogram 生成可执行文件myprogram 2、第二中编译方法, 分别编译各个文件:g++ -Wall -g -c wherewhen.c g++ -Wall -g -c countdown.c g++ -Wall -g -c main.c g++ -g wherewhen.o countdown.o main.o -lm -o myprogram

‘柒’ 如何用vs和makefile文件进行编译

运行cmd.exe (or command.com in win9x)->进到vc/bin目录->运行vc-vars32.bat->进到makefile 所在的目录->nmake /f makefile
从sourceforge上下载下来的libjpeg源代码中有一个makefile.vc的文件,可以通过nmake /f makefile.vc [nodebug=1]来编译libjpeg,但是只能编译静态库,如果需要编译dll以便在emacs等程序中使用的话,需要修改makefile.vc和jmorecfg.h文件。在makefile.vc文件中添加编译dll规则:
以下内容为程序代码:
libjpeg.lib: $(LIBOBJECTS) $(RM) libjpeg.lib lib -out:libjpeg.lib $(LIBOBJECTS) #
添加以下这行 libjpeg.dll: $(LIBOBJECTS) $(RM) libjpeg.dll link -dll -out:libjpeg.dll $(LIBOBJECTS) 在jmorecfg.h中添加#define _WIN32_#define JPEG_DLL 然后nmake /f makefile.vc nodebug=1就可以编译了。
将makefile复制为一个.mak文件,然后用VC打开即可!
.mak 就是一个makefile
可以指定怎样编译(命令行,必须先设置VC命令行环境)
vcvars32.bat可设置环境,在vc98/bin下 nmake /f XXXX.mak
如果有一个makefile就只要nmake就可以了。

‘捌’ 如何用makefile编译多个c文件

假设有下面几个c++文件:
wherewhen.h
wherewhen.c
countdown.h
countdown.c
包含了math.h,
需要连接库文件
main.c
主函数,
main.c
包含了两个头文件
wherewhen.h
and
countdown.h
1、第一种编译方法:
g++
-Wall
-g
wherewhen.c
countdown.c
main.c
-lm
-o
myprogram
生成可执行文件myprogram
2、第二中编译方法,
分别编译各个文件:
g++
-Wall
-g
-c
wherewhen.c
g++
-Wall
-g
-c
countdown.c
g++
-Wall
-g
-c
main.c
g++
-g
wherewhen.o
countdown.o
main.o
-lm
-o
myprogram

‘玖’ linux如何编写并使用makefile

1、先写Makefile编译出***.ko文件
模板如下,保存到命名为Makefile文件里,放到你代码的同级目录下
TARGET=my_proc.ko
LINUXDIR=/lib/moles/$(shell uname -r)/build
PWD=$(shell pwd)
obj-m :=
obj-m += my_proc.o

all: $(TARGET)
$(TARGET): $(OBJS)
make -C $(LINUXDIR) SUBDIRS=$(PWD) moles
clean:
rm -f moles.order Mole.symvers $(TARGET) *.mod.c *.o
rm -rf .tmp_versions .mod* Mole.markers
2、make
3、root权限下用命令插入模块
insmod my_proc.ko
4、可以用你写的应用程序打开、操作模块了
5、查看模块命令
lsmod
cat /proc/moles
modinfo my_proc.ko
6、root下卸载模块
rmmod

‘拾’ 编写一个简单的 makefile 文件

makefile文件里面主要有三种内容:


1.变量声明:
变量声明就是一种基本的严格字符替换的操作。
比如在前面声明了:objects=program.o foo.o utils.o
那么在后面出现的所有$(objects)或者${objects}都会被自动替换成上面的那个字符序列,而且是严格替换,即不带空格的。

2.映射法则

3.命令:
映射法则和命令通常都是联合起来组成这样的结构形式:
target... : prerequisites..
command

可以简单地理解为通过prerequisites,也就是先决的依赖文件,采取后面描述的相应的命令(这里的命令都是linux里的shell命令)command之后(一般是)生成了文件target。命令的前面都要按以下tab建留一段空白来表示它是命令。
有的target后面并没有先决条件,也就是后面的命令是无条件执行的。

makefile 文件c语言程序:
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{

printk(KERN_ALERT "Goodbye, cruel world\n");
}

mole_init(hello_init);
mole_exit(hello_exit);

makefile:
# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)

obj-m := hello.o
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else

KERNELDIR ?= /lib/moles/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) moles

endif

热点内容
怎样用windows服务器搭建网站 发布:2025-02-08 12:27:38 浏览:530
android获取音乐 发布:2025-02-08 12:26:05 浏览:961
存储的数据可以复制吗 发布:2025-02-08 12:20:22 浏览:852
scraino编程 发布:2025-02-08 11:59:41 浏览:265
我的世界服务器进不去该怎么办 发布:2025-02-08 11:47:41 浏览:236
linux的telnet 发布:2025-02-08 11:47:36 浏览:288
压缩袋打折 发布:2025-02-08 11:46:02 浏览:259
c语言结构体题目 发布:2025-02-08 11:46:01 浏览:339
如何svn限制一些外网不能访问 发布:2025-02-08 11:46:00 浏览:992
服务器外网ip咋配置 发布:2025-02-08 11:42:19 浏览:643