在Raspbian中创建Makefile [英] Creating a Makefile in Raspbian

查看:86
本文介绍了在Raspbian中创建Makefile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Raspbian(Raspberry Pi)中的C程序创建一个Makefile. 我的程序由一堆.c和.h文件组成.我看过无数的Makefile,但是我只是不理解它如何处理多个文件. Makefile中总是存在.o文件,但是据我了解,目标文件是编译的结果,所以我没有任何o.我正在尝试编译.c文件的文件.

I'm trying to create a Makefile for my C program in Raspbian (Raspberry Pi). My program consists of a bunch of .c and .h Files. I've looked at countless Makefiles, but I just don't unterstand how it works with multiple files. There are always .o files in the Makefile but as I understand object files are the result of compiling, so I dont have any o. Files as I am trying to compile my .c Files.

请向我解释这是如何工作的.

Please explain to me how this works.

谢谢.所以我尝试了这个,它开始编译,但是有错误多重定义".示例:

Thank you. So I tried this and it starts compiling but there are errors 'multiple definition'. Example:

这些是我的文件:

main.c main.h
calibration.c calibration.h
file.c file.h
frame.c frame.h
gamepad.c gamepad.h
gpio.c gpio.h
uart.c uart.h
types.h

这是我的makefile:

this is my makefile:

all: main

main: main.o calibration.o file.o frame.o gamepad.o gpio.o uart.o

%.o: %.c
    gcc -c -std=c99 -Wall $< -o $@ -lncurses

我可以在哪里放置"types.h"? 对于每个文件,我都会收到多个定义"错误

Where can i put 'types.h'? With every file I get errors 'multiple definitions'

推荐答案

make规则的基本语法是:

target … : prerequisites …
    recipe
    …
    …

在分号左侧是目标.目标是您的目标文件(.o).在分号的右边是创建此文件所需的文件.这些文件是源文件(.c).

On the left of the semicolon are the targets. The targets are your object files(.o). On the right of the semicolon are the files that you will need to create this file. Those files are the source files(.c).

让我们举一个基本的例子说明这种规则的样子.

Lets give a basic example of what such a rule could look like.

%.o: %.c
    gcc -c $< -o $@

%符号是通配符. %.o表示以.o结尾的所有内容.因此,如果要创建目标文件,可以说make file.o,而make会尝试找到一个规则以使其成为目标.这恰好是我刚刚作为示例显示的规则,因为file.o%.o匹配.

The % sign is a wildcard. %.o means everything that ends with .o. So, if you want to make an object file, you can say make file.o, and make will try to find a rule with which it can make this target. This happens to be the rule I just showed as an example, because file.o matches %.o.

然后按recipe.这将执行.通常,这是关于调用编译器(gcc),并将其提供给源文件以生成目标文件.这就是我们使用gcc -c $< -o $@所做的. $<$@分别表示targetprerequisites.

Then the recipe. This is what will be executed. Usually it's about invoking the compiler(gcc), and feeding it the source file to generate the object file. That's what we do with gcc -c $< -o $@. The $< and $@ mean target and prerequisites respectively.

那么,当您只是"想要构建程序时会发生什么?通常,您将键入make,它将生成.键入make时使用的默认规则是all.因此,如果您制定关于all的规则,则可以指定要创建的文件来构建程序.这样的规则的示例:

So, what happens when you 'just' want to build your program? You usually will type make, and it will build. The default rule that's used when you type make, is all. So, if you make a rule about all, then you can specify what files you want to create to build your program. Example of such a rule:

all: main

然后,当调用make时,它将找到该规则并发现它需要main.要创建main,您需要另一条规则:

Then, when make is invoked, it will find that rule and finds out it needs main. To create main you need another rule:

main: file.o

此规则说要构建main,您需要file.o.因此,当您将所有示例规则放在一起时,将得到以下信息:

This rule says that to build main, you need file.o. So, when you put all of the example rules together you get this:

all: main

main: file.o

%.o: %.c
    gcc -c $< -o $@

请注意,您可以指定多个文件,因此可以代替file.o,而可以说file.o main.o other_file.o等.如果指定了每个前提条件,只要他们可以找到创建它的规则,就会满足要求.

Note that you can specify more than one file, so instead of file.o, you can say file.o main.o other_file.o etc. Every prerequisite that you specify will be made, if they can find a rule to make it.

这篇关于在Raspbian中创建Makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆