-4

I pasted the wrong error in my question by mistake. Now I have fixed my question.

I'm having a problem compiling a program I wrote using math.h library. I can compile the program in my computer with visual 08 and codeblocks, but then I try to use my school IDE I get some errors.

/tmp/ccD2ZV6Q.o: In function `main': knit2.c:(.text+0x8d): undefined reference to `pow' knit2.c:(.text+0xce): undefined reference to `pow' knit2.c:(.text+0x11b): undefined reference to `pow' collect2: error: ld returned 1 exit status

I have to use this command to be able to compile it:

gcc knit2.c -lm -o knit2.exe

Is there anything I could do to my code to fix this so it can compile using gcc knit2.c

Kakarotto
  • 65
  • 7
  • 1
    typo: `\ ` --> `\n` – dbush May 24 '20 at 04:13
  • 1
    The error is nothing to do with `pow()`. You need the `-lm` option unless you eliminate the call to `pow()`. – Jonathan Leffler May 24 '20 at 04:24
  • Note that the C11 standard [§6.4.4.4 Character constants](http://port70.net/~nsz/c/c11/n1570.html#6.4.4.4) references a (non-normative) [footnote 77](http://port70.net/~nsz/c/c11/n1570.html#note77) which says: _… If any other character follows a backslash, the result is not a token and a diagnostic is required. …_ A blank is in the 'any other character' category. – Jonathan Leffler May 24 '20 at 04:29

1 Answers1

0

The mistake seems to be in your printf statement.

"You will be able to make %.f blankets\ "

Remove the backslash (\), that's what probably causing the error.

tikna
  • 34
  • 1
  • 3
  • Or replace the blank after the backslash with `n` to give `\n` line ending. – Jonathan Leffler May 24 '20 at 04:22
  • I pasted the wrong error in my question by mistake :/ `/tmp/ccD2ZV6Q.o: In function main': knit2.c:(.text+0x8d): undefined reference to pow' knit2.c:(.text+0xce): undefined reference to pow' knit2.c:(.text+0x11b): undefined reference to pow' collect2: error: ld returned 1 exit status ` – Kakarotto May 24 '20 at 15:35
  • As Jonathan mentioned, you need the -lm option to include math library in the build. See here - https://stackoverflow.com/questions/12824134/undefined-reference-to-pow-in-c-despite-including-math-h – tikna May 24 '20 at 19:19
  • Maybe your school IDE does not link that library by default, and that's why you need to give -lm option. The -o option and giving the name of executable after it should not be needed. – tikna May 24 '20 at 19:21
  • @tikna thank you!. seems to be that my school's IDE is not linking like you said. I had to turn in my project with a comment stating the situation. thx again. – Kakarotto May 24 '20 at 21:15