Program_name:链接器输入文件未使用,因为未完成链接 [英] Program_name: linker input file unused because linking not done

查看:61
本文介绍了Program_name:链接器输入文件未使用,因为未完成链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有6个程序:HOSpital.c,GenPazienti.c,Triage.c,Paziente.c,Prestazione.c和Reparto.c.

I have 6 programs: HOSpital.c, GenPazienti.c, Triage.c, Paziente.c, Prestazione.c and Reparto.c.

其中没有一个.

我该如何制作文件?

我尝试过:

all: HOSpital GenPazienti Paziente Prestazione Reparto Triage

HOSpital: HOSpital.o
    gcc -o HOSpital HOSpital.c

HOSpital.o: HOSpital.c 
    gcc -c HOSpital HOSpital.c

GenPazienti: GenPazienti.o
    gcc -o GenPazienti GenPazienti.c

GenPazienti.o: GenPazienti.c 
    gcc -c GenPazienti GenPazienti.c

Paziente: Paziente.o
    gcc -o Paziente Paziente.c

Paziente.o: Paziente.c
    gcc -c Paziente Paziente.c

Prestazione: Prestazione.o
    gcc -o Prestazione Pretazione.c

Prestazione.o: Prestazione.c 
    gcc -c Prestazione Prestazione.c

Reparto: Reparto.o
    gcc -o Reparto Reparto.c

Reparto.o: Reparto.c
    gcc -c Reparto Reparto.c

Triage: Triage.o
    gcc -o Triage Triage.c

Triage.o: Triage.c
    gcc -c Triage Triage.c

clean:
    rm -f *.o

但是,如果我更改了某些内容并键入"make",则会收到错误消息:"Program_name:链接器输入文件未使用,因为未完成链接"

But if i change something and i type "make" i get the error: "Program_name: linker input file unused because linking not done"

推荐答案

让我们举一个例子:

gcc -c HOSpital HOSpital.c

这将尝试将 HOSpital 用作 input 文件.

使用正确的选项来命名输出文件, -o 都正确命名.喜欢

Either use the correct option to name the output file, -o, and name it correctly. Like in

gcc -c -o HOSpital.o HOSpital.c

或者根本不指定输出文件名,那么编译器将使用输入源文件并将后缀 .c 更改为 .o .喜欢

Or don't specify the output file name at all, then the compiler will use the input source file and change the .c suffix to .o. Like in

gcc -c HOSpital.c

到处都是同样的问题.

最后没关系,使用规则即可构建目标文件,但实际上您没有使用目标文件:

Not that it matters in the end, the rule is used so the object file will be built, but you don't actually use the object file:

gcc -o HOSpital HOSpital.c

在这里,您直接使用源文件来创建程序.我想你打算用

Here you use the source file directly to create the program. I think you meant to use

gcc -o HOSpital.o HOSpital.o

与先前的问题一样,您会始终犯此错误.

And as with the previous problem, you make this mistake all over.

最后是一些一般性提示.

Finally some general tips.

首先,在启用更多警告的情况下进行构建.从长远来看,它将帮助您发现代码中的错误,并帮助您找出可能存在未定义行为的地方.我建议至少添加 -Wall -Wextra -pedantic .

First, build with more warnings enabled. It will help you in the long run to find mistakes in the code, and will help find out places where there's possible undefined behaviors. I recommend at least adding -Wall -Wextra -pedantic.

然后,您无需在makefile中显式列出所有目标文件及其规则. make 程序已经知道如何制作,例如目标文件通过 隐式规则 .

Then you don't need to list all the object files and their rules explicitly in the makefile. The make program already knows how to make e.g. object files through implicit rules.

最后一点意味着您可以将makefile缩短为类似的内容

That last point means you can shorten down the makefile to something like

CFLAGS = -Wall -Wextra -pedantic -pipe
LD = gcc
LDFLAGS = -pipe

HOSpital: HOSpital.o
    $(LD) $(LDFLAGS) -o $@ $^

GenPazienti: GenPazienti.o
    $(LD) $(LDFLAGS) -o $@ $^

Paziente: Paziente.o
    $(LD) $(LDFLAGS) -o $@ $^

Prestazione: Prestazione.o
    $(LD) $(LDFLAGS) -o $@ $^

Reparto: Reparto.o
    $(LD) $(LDFLAGS) -o $@ $^

Triage: Triage.o
    $(LD) $(LDFLAGS) -o $@ $^

clean:
    -rm -f *.o

变量$@是规则的目标,变量$^是所有的先决条件.例如

The variable $@ is the target of the rule, and the variable $^ is all prerequisites. For e.g.

HOSpital: HOSpital.o

变量 $ @ HOSpital $ ^ HOSpital.o .

这篇关于Program_name:链接器输入文件未使用,因为未完成链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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