gccmakefileexecutable

Make file not working?


Solution found. See below:

I'm trying to get my makefile to compile three c programs into one executable, but I get the following error:

cachesim.o: could not read symbols: File in wrong format

Yes, I'm using make clean every time I use it. The make file is as follow

CC     = gcc
CFLAGS = -Wall -m32 -O -g

all:  cachesim cache trace_file_parser 
gcc -o cachesim cachesim.o cache.o trace_file_parser.o

cachesim:       cachesim.c
        $(CC) -c -o cachesim.o cachesim.c $(CFLAGS)

cache:          cache.c
        $(CC) -c -o cache.o cache.c $(CFLAGS)

trace_file_parser:  trace_file_parser.c
        $(CC) -c -o trace_file_parser.o trace_file_parser.c $(CFLAGS)

clean:
rm -f *.o

I cannot figure out why this is....

I'm using make clean every time.

Attempting to compile:

[katiea@mumble-15] (34)$ make clean
rm -f *.o
[katiea@mumble-15] (35)$ ls
cache.c   cache.h     cachesim.c~      gcc_trace  Makefile~     trace_file_parser.c
cache.c~  cachesim.c  cache_structs.h  Makefile   strgen_trace  trace_file_parser.h
[katiea@mumble-15] (36)$ make
gcc -c -o cachesim.o cachesim.c -Wall -m32 -O -g
gcc -c -o cache.o cache.c -Wall -m32 -O -g
gcc -c -o trace_file_parser.o trace_file_parser.c -Wall -m32 -O -g
gcc -o cachesim cachesim.o cache.o trace_file_parser.o
cachesim.o: could not read symbols: File in wrong format
collect2: ld returned 1 exit status
make: *** [all] Error 1

SOLUTION

CC     = gcc

CFLAGS = -Wall -m32 -O -g

all:  cachesim.c cache.c trace_file_parser.c
$(CC) -o cachesim cachesim.c cache.c trace_file_parser.c $(CFLAGS)

cachesim:       cachesim.c
        $(CC) -c -o cachesim.o cachesim.c $(CFLAGS)

cache:          cache.c
        $(CC) -c -o cache.o cache.c $(CFLAGS)

trace_file_parser:  trace_file_parser.c
        $(CC) -c -o trace_file_parser.o trace_file_parser.c $(CFLAGS)

clean:
rm -f *.o

Solution

  • Please read an intro to makefiles. This looks like homework to me.

    One of the most basic tenets of makefiles is that the target should be the actual file you're building. These rules are all bogus:

    cachesim:       cachesim.c
             $(CC) -c -o cachesim.o cachesim.c $(CFLAGS)
    

    (etc.) because the target is cachesim but the recipe (command line) builds the file cachesim.o.

    Your makefile can be written as easily as this (taking advantage of make's built-in rules):

    CC      = gcc
    CFLAGS  = -Wall -m32 -O -g
    LDFLAGS = -m32 -O -g
    
    cachesim: cachesim.o cache.o trace_file_parser.o
    
    clean:
            rm -f *.o
    

    That's all you need.

    As for your error, it seems to me that the file cachesim.o must be in some bizarre format, maybe from back before you had the makefile set up properly.

    If you run make clean then make again, do you get the same error? If so please show the compile and link lines.

    ETA: use the -m32 flag on the link line as well as the compile line, if you want to create a 32bit program.