fy_iceworld
fy_iceworld

Reputation: 666

gcc - file not recognized: File format not recognized

I'm trying to get a small project compiled but am getting this error, I've been searching around and people get this error mainly because of incorrect file extension, but I don't really think that's the cause here:

gcc -c -W -Wall -ggdb -I. router.c -o router.o
router.c:106: warning: unused parameter ‘hname’
router.c: In function ‘flood_neighbors’:
router.c:464: warning: unused variable ‘bytes_rcvd’
router.c: At top level:
router.c:536: warning: unused parameter ‘fd’
gcc -c -W -Wall -ggdb -I. link_info.h -o link_info.o
gcc -c -W -Wall -ggdb -I. route.h -o route.o
gcc -c -W -Wall -ggdb -I. sequence.h -o sequence.o
gcc -W -Wall -ggdb -I. router.o link_info.o route.o sequence.o -o router
link_info.o: file not recognized: File format not recognized
collect2: ld returned 1 exit status
make: *** [router] Error 1

and my make file looks like:

CC = gcc
INC = -I.
FLAGS = -W -Wall -ggdb

router: router.o link_info.o route.o sequence.o
        $(CC) $(FLAGS) $(INC) $^ -o $@

router.o: router.c
        $(CC) -c $(FLAGS) $(INC) $< -o $@

sequence.o: sequence.h sequence.h
        $(CC) -c $(FLAGS) $(INC) $< -o $@

link_info.o: link_info.h link_info.c
        $(CC) -c $(FLAGS) $(INC) $< -o $@

route.o: route.h route.c
        $(CC) -c $(FLAGS) $(INC) $< -o $@

What I'm confused on is the rules for three object files are of the same format, but why only the link one yells? Thanks a lot!

Upvotes: 6

Views: 36652

Answers (3)

Mike Housky
Mike Housky

Reputation: 4069

Move link_info.o to the end of the list in the router: dependencies and I suspect you'll blow up on route.o instead.

You seem to be compiling a headers as C files. I have no idea what GCC does in that case. In your makefile rules, the .c file must be the first dependency if you are going to use $< to generate the source file to compile.

The link_info: and route: rules have the .c and .h files reversed and the sequence: rule lists the .h file twice!

Upvotes: 2

sashang
sashang

Reputation: 12194

With recent versions of make you can remove all the object rules because make uses a default rule to build an object from .c or .cpp files. Just leave the rule to build the final executable in place.

For example, change the file so it contains this line only:

router: router.o link_info.o route.o sequence.o
    $(CC) $(FLAGS) $(INC) $^ -o $@

Upvotes: 2

CCoder
CCoder

Reputation: 2335

I would like to suggest few edits in your makefile.

CC = gcc
INC = -I.
FLAGS = -W -Wall -ggdb

router: router.o link_info.o route.o sequence.o
        $(CC) $(FLAGS) $(INC) $^ -o $@

router.o: router.c
        $(CC) -c $(FLAGS) $(INC) $< -o $@

sequence.o: sequence.h sequence.h //Where is the c file ?
        $(CC) -c $(FLAGS) $(INC) $< -o $@

link_info.o: link_info.h link_info.c //Change the order. Put c file first and then the header
        $(CC) -c $(FLAGS) $(INC) $< -o $@

route.o: route.h route.c //Same as above
        $(CC) -c $(FLAGS) $(INC) $< -o $@

Upvotes: 5

Related Questions