1

I am using pow function in C and included the math.h library too
The following is the error that I am getting :

/usr/bin/ld: /tmp/ccUkOL31.o: in function `main': a1B.c:(.text+0xf3): undefined reference to 'pow' collect2: error: ld returned 1 exit status

Although I read on StackOverflow that linking is required. But that's weird. Is there any way to avoid that extra step when we are compiling with gcc. g++ seems to be doing it automatically.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
nanu
  • 33
  • 1
  • 6
  • See also: https://stackoverflow.com/q/4188409/253056 – Paul R Jul 13 '21 at 09:41
  • 3
    I would like to remark that incorrect use of jargon is often an indication for a lack of comprehension. You did not include the `math.h` library, you included the `math.h` header file. The header file described the API for the math library, which is likely located at something like `/lib/libm.so.6`. That library needs to be linked, it's not weird. It's probably weirder to assume that everything is in libc (which is automatically linked). If your problem is with executing commands manually, then don't. Use make or cmake. Typing `make` is always 1 step. – Cheatah Jul 13 '21 at 10:15

1 Answers1

0

Is there any way to avoid that extra step when we are compiling with gcc?

The way I do it is having a script that invokes gcc (I have ~/bin in my SPATH)

$ cat ~/bin/mycc
#!/bin/sh
gcc -std=c11 -pedantic -Wall $* -lm
$ mycc example1.c example2.c
pmg
  • 106,608
  • 13
  • 126
  • 198