File format not recognized; as a linker script (question in makefile)

I am new to Makefiles and I am trying to create an executable file of 3 files, file1.c, file2.c and file1.h into an exFile executable. Here is 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 searched the Internet for make files in this format, but I came up empty-handed, so I was wondering if anyone could help. When it tries to compile, I get:

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

I compiled programs using assembly files, but I'm not sure what to do with the c files or file1.h file. file1.c includes file1.h, so I need to link them (I think?). Any suggestions or links to the link will be appreciated

+4
source share
2 answers

You have two problems with your gcc command line. First, you specify the -S flag, which causes gcc to emit assembly code, not object code. Secondly, you miss the -c flag, in which gcc compiles the file into an object file, but does not bind it. If you simply remove -S and do not change anything, you will get an executable program named file1.o and another named file2.o , not two object files.

Besides these errors, you can simplify your makefile with the help of template rules. I suggest you try the following:

 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 uses more GNU make built-in functions:

 CC=gcc CFLAGS=-Wall -g -m32 all: exFile exFile: file1.o file2.o $(LINK.c) $^ -o $@ file1.o: file1.h 
+6
source

The -S switch in gcc tells it to output the assembler like this:

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

Inserts assembler into file1.o , but you want to apparently compile file1.c into object code:

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

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

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

0
source

Source: https://habr.com/ru/post/1347028/


All Articles