1

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 !

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • 4
    The above command only create an object file. What command create the executable? – dbush Mar 13 '17 at 17:35
  • `math.h` is a **header** for the math lib. The name of the lib depends on your platform and is required for linking. `-ansi` and `-std=c99` exclude each other. ANSI-C is not C99. Use the latter (better: standard C which would be C11), ANSI-C is outdated since 18 years. – too honest for this site Mar 13 '17 at 17:35
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – too honest for this site Mar 13 '17 at 17:36
  • 1
    Please show the rest of your makefile. The `-lm` belongs elsewhere in your makefile. – Jabberwocky Mar 13 '17 at 17:40
  • Your link doesn't work (of course, it's a _local_ link that works only on _your_ computer). Please edit your question and post your make file _there_. – Jabberwocky Mar 13 '17 at 17:48
  • my executable command is : rendu1.x : error.o lecture.o fourmi.o \n gcc rendu1.o error.o fourmi.o lecture.o – Ulysse Touchais Mar 13 '17 at 18:01
  • You have to put `-lm` to the line where executable is linked. – 0andriy Mar 13 '17 at 20:12
  • List libraries after object files in link commands. The command you show doesn't trigger the error you show. A similar command for linking that lists `-lm` before `fourmi.o` on the command line might generate the error you show. – Jonathan Leffler Mar 13 '17 at 21:31

1 Answers1

2

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.

deamentiaemundi
  • 5,502
  • 2
  • 12
  • 20
  • Is there an elegant way to prevent having to write my_exe: my_exe.o $(CC) $(CFLAGS) $< -o $@ $(LIBS) for all programs in my little project? ** found it! you need to assign the libraries options to LDLIBS and not LIBS. – rew May 10 '20 at 06:41
  • @rew Hu? Ah, that one. `LDLIBS` is for the linker `ld` which might not be the one you want/can use. My version is intentionally a general one. There is also a difference between GNU-makefiles (e.g.: Linux) with many GNU extensions and Unix-makfiles (e.g. BSD). If you want the GNU extensions you need to install GNU-make (`gmake` in most BSDs). – deamentiaemundi May 10 '20 at 19:19
  • I want a tool like "make" to help me. If I have to write out the complete command for every compile-step, then I can just as well write a shell script (bat file in DOS-speak). It is probably a personal preference, but I like the Makefile to be compact. And easy to expand. I have a few projects where I have a bunch of simple programs managed by that one Makefile. Then I very much prefer adding just the one extra program to BINS=... instead of having to add stuff like the linker line explicitly. – rew May 27 '20 at 13:44