Reputation: 53
in my project I need to use the math.h lib. So in my makefile I added -lm to the file in question. Here is the line :
$(CC) $(CFLAGS) -c fourmi.c -lm -o fourmi.o
I compile with gcc and my CFLAGS has -Wall -ansi -std=c99
I thought it would be correct but when I compile I am told :
undefined reference to pow/sqrt...
I know a lot of posts talk about this but none of those helped me. Does anybody has a clue of what could possibly be wrong ?
Thanks a lot !
Upvotes: 1
Views: 10407
Reputation: 5525
It seems from you comment that you underestimated the intricacies of a Makefile
.
A simple example:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(void){
printf("The third root of 5 is about: %.20g\n",pow(5.0,1/3.0));
exit(EXIT_SUCCESS);
}
A simple, although not the simplest, Makefile
for it could be
# Just what I regularily use, YMMV, of course
CFLAGS += -O3 -g3 -W -Wall -Wextra -Wuninitialized -Wstrict-aliasing -std=c11
# Libmath
LIBS += -lm
# the executable is made from one or more object files
# if you have more, add them here
fourmi_executable: fourmi.o
$(CC) $(CFLAGS) fourmi.o -o fourmi $(LIBS)
# an object file is made from one sourcecode file)
# if you have more, add more of these constructs
fourmi.o: fourmi.c
$(CC) $(CFLAGS) -c fourmi.c
clean:
rm *.o
rm fourmi_executable
(Don't forget: the whitespace inserts are made by one tab, not spaces!)
This Makefile
is very simple and not good for more than a handful of sourcefiles or anything more complicated than just compiling into one executable but should be useful as a start.
Upvotes: 2