1

I have the following C code:

#include <stdio.h>
#include <math.h>
int main(){
   printf("Square = %d", pow(4,2));
   return 0;
}

Now if I run this in the shell. I would first compile with: gcc -lm file.c -o a.out and then run with ./a.out.

If I run the above code in org mode within #+BEGIN_SRC C and #+END_SRC, I would get an error "undefined reference to pow".

Reason is that the compiling command has no link -lm. Is there a way to add this to org-babel execution of C code?

mle0312
  • 295
  • 1
  • 8

1 Answers1

1

Use the :flags or :libs header to pass flags to the compiler (or libraries to the linker):

#+begin_src C :libs -lm
#include <stdio.h>
#include <math.h>
int main(){
   printf("Square = %f", pow(4,2));
   return 0;
}

#+end_src

See the Working with source code/Languages section of the Org mode manual. That contains a link to a page on Worg with links to details about specific headers for each of many languages: the specific page on C/C++/D can be found here.

Note that your program has a bug: pow() returns a double, so you need %f format, not %d.

NickD
  • 27,023
  • 3
  • 23
  • 42