3

what i'm doing wrong? And can you send some helpful links to make my work with makefiles easier and better?

get_next_line.o: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
Makefile:27: recipe for target 'gnl' failed
make: *** [gnl] Error 1

+++

SRC = gnl.c
OBJ = $(SRC:.c=.o)
INCLUDES = gnl.h
NAME = gnl
CFLAGS = -Wall -Werror -Wextra
CC = gcc
DIR = LIBFT

%.o: %.c $(SRC) $(INCLUDES)
        $(CC) $(CFLAGS) -c $<

all: $(NAME)

$(NAME): $(OBJ)
        make -C $(DIR)
        $(CC) $(CFLAGS) -o $(NAME) $(OBJ) -L. $(DIR)/libft.a
clean:
        make clean -C $(DIR)
        rm -f $(OBJ)

fclean:
        make fclean -C $(DIR)
        rm -f $(NAME)

re: fclean all
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
EmanRuoy
  • 77
  • 1
  • 2
  • 9
  • Does the makefile contain line numbers? – Weather Vane Nov 23 '17 at 20:33
  • nope,sry, it's copied from Vim – EmanRuoy Nov 23 '17 at 20:34
  • Looks like a rotten object file. Do a `make clean` and try again. – deamentiaemundi Nov 23 '17 at 20:56
  • 3
    I don't see any reference to `get_next_line.c` - Is this your real Makefile? If yes, then the problem might be in the `LIBFT` Makefile. Write a hello world in test.c+test.h and comment the `make -C $(DIR)` to check it. – jweyrich Nov 23 '17 at 21:04
  • Give that the error is reported from line 27 of the makefile, and refers to an object file that is not a target in the *25 line* makefile posted, this is clearly not the makefile that generated the error shown! You are not helping yourself by posting misleading information - answers will be a guess at best. – Clifford Nov 23 '17 at 22:23

4 Answers4

7

This is not a make error by itself. It says

get_next_line.o: file not recognized: File format not recognized

so that is your problem. Somehow you managed to have a .o file in your directory that is corrupted. Remove it and things will go better.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
1

You have misunderstood the error message. The error message is referring to the file get_next_line.o, not the makefile.

It is the linker (ld) that is reporting the error not make. The command that has failed is that for the target gnl.

Clifford
  • 88,407
  • 13
  • 85
  • 165
1

It can even be simpler... It just happened to me with a Makefile. In the line (in the Makefile) that creates the executable, where there are different names of objects .o, there was a typo. I left the name of the file .cu (CUDA format) instead of .o.

0

I ran into this error recently, and I have a couple of suggestions that might help. My problem was not a bad .o file, but rather a 32-bit installation rather than a 64-bit installation. If this is the case for you, too, you might get more complete functionality by trying this solution.

In my case, the makefile(s) in question needed different CFLAGS depending on whether the installation was 64-bit or 32-bit. Here are some lines from the README of the project I was trying to make.

By default, the C/C++ software are compiled in 32 bits with the options (-Os) but can be compiled in 64 bits, -m64 is added to the CFLAGS variable in <certain makefiles in the project are listed>

My suggestion is to first try adding -m64 to your CFLAGS. If that doesn't work, delete the -m64 and replace it with -Os.

That is, first try having the following line:

CFLAGS = -g3 -Wall -Wextra -fPIC -DREPLICATION_ENABLED -DJOURNALING_ENABLED -m64

Then, from the command line, run

make clean

Followed by whatever make commands you use for your install.

If that doesn't work, change the line in question to

CFLAGS = -g3 -Wall -Wextra -fPIC -DREPLICATION_ENABLED -DJOURNALING_ENABLED -Os

Then make clean and the other make stuff.

If some of the C objects are 64-bit and some are 32-bit (I don't know if such a situation actually exists), you might have to do something different.

This worked in my case, details of which you can see here.

Please comment to let me know if it works for you.

bballdave025
  • 1,347
  • 1
  • 15
  • 28