I'm trying to run current code block
#+HEADERS: :includes <math.h> <stdio.h> :flags -lm
#+HEADERS: :var x=1.0 :var y=4.0 :var z=10.0
#+begin_src C :tangle trying
double pi = 4*atan(1);
double r, theta, phi;
r = sqrt(x*x+y*y+z*z);
theta = acos(z/r) * 180.0/pi;
phi = atan2(y,x) * 180.0/pi;
printf("%f %f %f", r, theta, phi);
#+end_src
And when I try to execute this code, I got an error:
/tmp/ccPySR8b.o: In function 'main':
C-src-uM2yPq.c:(.text+0x5a): undefined reference to 'sqrt'
C-src-uM2yPq.c:(.text+0x75): undefined reference to 'acos'
C-src-uM2yPq.c:(.text+0xb0): undefined reference to 'atan2'
collect2: error: ld returned 1 exit status
I guess, this happened, because babel put the flag before filename gcc -lm code.c
, instead gcc code.c -lm
. I don't know how to fix it.
Org mode version 9.1.4
GNU Emacs 27.0.50
In fact, I tested this code block on different PCs, and this case have 100% reproducibility.
UPDATE
-lm
must be used with :libs
option, not :flags
, thx @NickD
#+HEADERS: :includes <math.h> <stdio.h> :libs -lm
with:libs
instead of:flags
; that should put the-lm
at the end where your gcc will not complain. But it would still be interesting to find out why your gcc complains, whereas other gcc's do not.