Trying to use (make) with math.h
Matthew Harrington
With gcc command I use the -lm flag with math.h. I trying to use make and I'm getting errors. Is there an lm trick with make and makefiles?
21 Answer
If you are new to make and you can compile your code manually on the command line, then I recommend the following steps:
- Create a Makefile which basically does exactly what you would do on the command line, e.g.,
mybinary: mysourcecode.c gcc -o mybinary -lm mysourcecode.c - Split up compiling and linking your code, e.g.,
mybinary: mysourcecode.o ld -o mybinary -lm mysourcecode.o mysourcode.o: mysourcecode.c gcc -c -o mysourcecode.o mysourcecode.c - Make general rules and add use all the fancy features of
make, e.g.,
.PHONY: clean
objects := $(subst .c,.o,$(wildcard *.c))
target := mybinary
$(target): $(objects) ld -o $@ -lm $^ %.o: %.c gcc -c -o $@ $< clean: -rm $(target) $(objects)