Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File format not recognized; treating as linker script using GCC

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

like image 993
user695978 Avatar asked Apr 07 '11 03:04

user695978


2 Answers

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
like image 133
Eric Melski Avatar answered Nov 13 '22 06:11

Eric Melski


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.

like image 43
mu is too short Avatar answered Nov 13 '22 08:11

mu is too short