5

I'm having some trouble trying to compile a program that uses exp function on Ubuntu. I get this error from gcc:

selied@Apolo:~/Dropbox/practicas UAM/Neuro/practica3$ make
gcc -lm -o retropropagacion retropropagacion.o 
retropropagacion.o: In function `main':
/home/selied/Dropbox/practicas UAM/Neuro/practica3/retropropagacion.c:177: undefined     reference to `exp'
/home/selied/Dropbox/practicas UAM/Neuro/practica3/retropropagacion.c:186: undefined reference to `exp'
/home/selied/Dropbox/practicas UAM/Neuro/practica3/retropropagacion.c:297: undefined reference to `exp'
/home/selied/Dropbox/practicas UAM/Neuro/practica3/retropropagacion.c:306: undefined reference to `exp'
collect2: ld devolvió el estado de salida 1
make: *** [retropropagacion] Error 1

Here I show you my makefile.

CC      = gcc
LDLAGS  = -lm
CFLAGS  = -Wall -g
EXE     = retropropagacion normalizar
OBJ     = 
INC     = 

compile    : $(EXE)

clean  :
    @echo Borrando archivos temporales...
    rm -f *~ *.o core $(EXE)

help    :
    @echo   

backpropagation : 
    ./retropropagacion entrada.txt 0 0 salida.txt

and : 
    ./retropropagacion and.dat 0 0 salida_and.txt

$(EXE) : % : %.o $(OBJ)
    $(CC) $(LDLAGS) -o $@ $@.o $(OBJ)

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

Also I have include at the top of my header file and it works on another computer.

Do you know what's happening?

Selied
  • 53
  • 1
  • 1
  • 3

2 Answers2

11
$(CC) $(LDLAGS) -o $@ $@.o $(OBJ)

should be

$(CC) -o $@ $@.o $(OBJ) $(LDLAGS)

Whether -l flags can be given before object files depends on the GCC version.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • Whoever downvoted that answer should themselves be downvoted! If there really was a GCC version that allowed this then that was a bug. – ams Dec 13 '11 at 15:59
  • This has solved my problem with an unrelated program which only uses a makefile and no configure script. – Nick Jan 23 '12 at 15:24
  • I am trying to solve this issue for some time. Why is make putting by default the `LDFLAGS` in front of object files, if I omit the discussed line completely? The same code works fine on Fedora, but does not compile on Ubuntu. Is there any way to control this behaviour except overwriting it in every Makefile? – Jakuje Nov 30 '15 at 19:38
2

Never mind. For further interested about this issue or struggling also too long, also line

LDLAGS  = -lm

should be written as

LDLIBS = -lm

because LDLIBS are put after the object files, unlike the LDFLAGS, which ends in front of them in default make template, as documentation hints.

Jakuje
  • 24,773
  • 12
  • 69
  • 75