开发者

File format not recognized; treating as linker script using GCC

开发者 https://www.devze.com 2023-02-22 10:17 出处:网络
I am pretty new to Makefiles and i am trying to build an executable from 3 files,file1.c, file2.c, and file1.h into an executable called exFile. Here\'s what I got:

I am pretty new to Makefiles and i am trying to build an executable from 3 files, file1.c, file2.c, and file1.h into an executable called exFile. Here's what I got:

all: exFile
exFile: file1.o file2.o 
    gcc -Wall -g -m32 repeat.o show.o -o repeat

file1.o: file1.c file1.h
    gcc -Wall -g -m32 -S file1.c -o file1.o

file2.o开发者_如何学编程: file2.c 
    gcc -Wall -g -m32 -S file2.c -o file2.o

I've searched the web for makefiles in this format, but i came up empty handed so i was wondering if someone can help. When it tries to compile i get:

usr/bin/ld:file1.o:1: file format not recognized; treating as linker script

I've compiled programs using assembly files but I'm not to sure what to do with c files or the file1.h file. file1.c includes file1.h so i have to link them (I think?). Any suggestions or links to a reference would be appreciated


You have two problems with your gcc command-line. First, you're specifying the -S flag, which causes gcc to emit assembly code, rather than object code. Second, you're missing the -c flag, which tells gcc to compile the file to an object file, but not link it. If you just remove -S and change nothing else, you'll end up with an executable program named file1.o and another named file2.o, rather than two object files.

Besides those errors, you could simplify your makefile by the use of pattern rules. I suggest you try the following instead:

all: exFile
exFile: file1.o file2.o 
    gcc -Wall -g -m32 $^ -o $@

%.o: %.c
    gcc -Wall -g -m32 -c $< -o $@

file1.o: file1.h

Or, as EmployedRussian points out, you can go with something even more minimal that leverages more of the built-in features of GNU make:

CC=gcc
CFLAGS=-Wall -g -m32

all: exFile

exFile: file1.o file2.o 
        $(LINK.c) $^ -o $@

file1.o: file1.h


The -S switch to gcc tells it to output assembler so this:

gcc -Wall -g -m32 -S file1.c -o file1.o

Is putting assembler into file1.o but you want, presumably, to compile file1.c into object code:

gcc -Wall -g -m32 file1.c -o file1.o

When the linker gets your file1.o it is confused because file1.o is assembler when the linker is expecting object code, hence your error.

So get rid of the -S switches for file1.o and file2.o.

0

精彩评论

暂无评论...
验证码 换一张
取 消