1

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

  • 1
    I can not reproduce, Emacs 25.2.2, Org 9.1.2. Concerning gcc, both gcc -lm code.c and gcc code.c -lm work -> this is not the cause of your problem. – Picaud Vincent Dec 9 '17 at 22:18
  • 1
    I can not reproduce it with GNU Emacs 27.0.50 and Org mode version 9.1.4 (release_9.1.4-206-g4b80c6). I get #+RESULTS:\n: 10.816654 22.406871 75.963757. – NickD Dec 10 '17 at 3:01
  • What does the C file (presumably /tmp/C-src-uM2yPq.c) look like? – NickD Dec 10 '17 at 4:53
  • Nick, like that – Роман Зайруллин Dec 10 '17 at 9:20
  • 1
    Also try #+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. – NickD Dec 11 '17 at 23:13
0

I think this problem is caused by an old version of gcc. I tangled your code to create a source file named trying.c. It compiles just fine with the -lm flag before or after the filename with a recent gcc:

gcc -lm trying.c # compiles without error
gcc trying.c -lm # compiles without error
gcc -v # -> gcc version 8.2.0 (Debian 8.2.0-3) 

When I try to run the same code on an older Ubuntu distribution, it fails as you describe:

gcc -lm trying.c
/tmp/cc7tRgsC.o: In function `main':
trying.c:(.text+0x5b): undefined reference to `sqrt'
trying.c:(.text+0x7a): undefined reference to `acos'
trying.c:(.text+0xb5): undefined reference to `atan2'
collect2: error: ld returned 1 exit status
gcc trying.c -lm # compiles without error
gcc -v # -> gcc version 4.8.5 (Ubuntu 4.8.5-4ubuntu8~14.04.2) 

Your original org-mode file compiles just fine on a computer with a recent version of gcc.

As you've discovered, setting the -lm argument using the libs option rather than the flags gets you around the issue. This might be a bug in org-mode, but I'm not familiar enough with C programming to be sure about the difference between libs vs flags.

0

In org-mode we must pass multiple header files as a list, see code below:

  #+header: :var x=1.0 :var y=4.0 :var z=10.0
  #+BEGIN_SRC C :results output  :flags -lm :includes '(<math.h> <stdio.h>)
  double r=0.0;
  double theta=0.0;
  double phi=0.0;
  double pi = 4.0 * atan(1);
  r = sqrt(x*x + y*y + z*z);
  theta = acos(z/r) *180.0/pi;
  printf("%f %f %f", r, theta, pi);
  #+END_SRC  

  #+RESULTS:
  : 10.816654 22.406871 3.141593

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.