Re: [O] org-babel C math.h issue
Seems good. I tested it. As a refinement, the associated documentation should be updated (but that is a separate patch). http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-C.html Thierry Le 11/03/2016 20:14, Oz Ben-Ami a écrit : Thanks for the response. On my machine, I get the undefined references even with GCC 5 (gcc version 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu2)), if I put -lm before the source file name. As far as a patch, I can try but this would be my first time really playing with org-mode code. As far as I can see, I would need to add the following lines in ob-C.el: L133: (libs (cdr (assoc :libs params))) (libs (mapconcat 'identity (if (listp libs) libs (list libs)) " ")) L142: (format "%s -o %s %s %s %s" ;; (added %s) L147: libs Am I missing anything? Oz On Fri, Mar 11, 2016 at 1:52 PM, Thierry Banelwrote: To summarize this thread: It seems there was an issue long ago. Version 5 of GCC magically fixed it. But it is still here for older compilers. - To debug, execute the following piece of lisp code: (defadvice org-babel-eval (before xxx (cmd body)) "" (message "org-babel-eval : %s" cmd)) (ad-activate 'org-babel-eval) The actual compilation command will be displayed in the *Messages* buffer. With your example, we get gcc -o /tmp/babel-8412zIw/C-bin-8412IZR -lm /tmp/babel-8412zIw/C-src-84127OL.c -- You are right, Oz, this can be fixed in line 147. But :flags is intended for flags like -g or -O, which need to appear before the source file. Probably we are missing an additional flag, which could be named :libs Then your example would be: #+BEGIN_SRC C :includes '( ) :libs -lm int i=9; printf("%d\n",(int)sqrt(i)); #+END_SRC -- Oz, do you think you would be able to provide a patch? The way to contribute is documented here: http://orgmode.org/worg/org-contribute.html#orgheadline1 Thanks for reporting. Thierry Le 06/03/2016 23:44, Oz Ben-Ami a écrit : > Hi, > > I'm a new org user, and I'm sorry if I'm missing something obvious. > When executing a C code snippet with org-babel, that contains a math > function, I get the famous "undefined reference" errors. I tried > adding ":flags -lm", but that doesn't help. Looking at the code, it > seems the -lm flag is inserted in the wrong place, before the source > file. An easy change would be in line 147 of ob-C.el version > 8.3.4-634, moving "flags" to after the source file. This seems to > work, but I don't know if it would break anything else. > > A minimal working example, attached, includes the following snippet: > > #+BEGIN_SRC C :includes '( ) :flags -lm > int i=9; > printf("%d\n",(int)sqrt(i)); > #+END_SRC > > Note the issue disappears if constants are directly used rather than > variables, presumably because the function call is optimized away > entirely. > > Any thoughts are appreciated. > > Oz
Re: [O] org-babel C math.h issue
Thanks for the response. On my machine, I get the undefined references even with GCC 5 (gcc version 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu2)), if I put -lm before the source file name. As far as a patch, I can try but this would be my first time really playing with org-mode code. As far as I can see, I would need to add the following lines in ob-C.el: L133: (libs (cdr (assoc :libs params))) (libs (mapconcat 'identity (if (listp libs) libs (list libs)) " ")) L142: (format "%s -o %s %s %s %s" ;; (added %s) L147: libs Am I missing anything? Oz On Fri, Mar 11, 2016 at 1:52 PM, Thierry Banelwrote: > To summarize this thread: > It seems there was an issue long ago. > Version 5 of GCC magically fixed it. > But it is still here for older compilers. > > - > > To debug, execute the following piece of lisp code: > > (defadvice org-babel-eval (before xxx (cmd body)) > "" > (message "org-babel-eval : %s" cmd)) > (ad-activate 'org-babel-eval) > > The actual compilation command will be displayed in the *Messages* buffer. > > With your example, we get > >gcc -o /tmp/babel-8412zIw/C-bin-8412IZR -lm > /tmp/babel-8412zIw/C-src-84127OL.c > > -- > > You are right, Oz, this can be fixed in line 147. > But :flags is intended for flags like -g or -O, which need to appear > before the source file. > > Probably we are missing an additional flag, which could be named :libs > Then your example would be: > > #+BEGIN_SRC C :includes '( ) :libs -lm > int i=9; > printf("%d\n",(int)sqrt(i)); > #+END_SRC > > -- > > Oz, do you think you would be able to provide a patch? > The way to contribute is documented here: > http://orgmode.org/worg/org-contribute.html#orgheadline1 > > Thanks for reporting. > Thierry > > > Le 06/03/2016 23:44, Oz Ben-Ami a écrit : > > Hi, > > > > I'm a new org user, and I'm sorry if I'm missing something obvious. > > When executing a C code snippet with org-babel, that contains a math > > function, I get the famous "undefined reference" errors. I tried > > adding ":flags -lm", but that doesn't help. Looking at the code, it > > seems the -lm flag is inserted in the wrong place, before the source > > file. An easy change would be in line 147 of ob-C.el version > > 8.3.4-634, moving "flags" to after the source file. This seems to > > work, but I don't know if it would break anything else. > > > > A minimal working example, attached, includes the following snippet: > > > > #+BEGIN_SRC C :includes '( ) :flags -lm > > int i=9; > > printf("%d\n",(int)sqrt(i)); > > #+END_SRC > > > > Note the issue disappears if constants are directly used rather than > > variables, presumably because the function call is optimized away > > entirely. > > > > Any thoughts are appreciated. > > > > Oz > > >
Re: [O] org-babel C math.h issue
To summarize this thread: It seems there was an issue long ago. Version 5 of GCC magically fixed it. But it is still here for older compilers. - To debug, execute the following piece of lisp code: (defadvice org-babel-eval (before xxx (cmd body)) "" (message "org-babel-eval : %s" cmd)) (ad-activate 'org-babel-eval) The actual compilation command will be displayed in the *Messages* buffer. With your example, we get gcc -o /tmp/babel-8412zIw/C-bin-8412IZR -lm /tmp/babel-8412zIw/C-src-84127OL.c -- You are right, Oz, this can be fixed in line 147. But :flags is intended for flags like -g or -O, which need to appear before the source file. Probably we are missing an additional flag, which could be named :libs Then your example would be: #+BEGIN_SRC C :includes '( ) :libs -lm int i=9; printf("%d\n",(int)sqrt(i)); #+END_SRC -- Oz, do you think you would be able to provide a patch? The way to contribute is documented here: http://orgmode.org/worg/org-contribute.html#orgheadline1 Thanks for reporting. Thierry Le 06/03/2016 23:44, Oz Ben-Ami a écrit : > Hi, > > I'm a new org user, and I'm sorry if I'm missing something obvious. > When executing a C code snippet with org-babel, that contains a math > function, I get the famous "undefined reference" errors. I tried > adding ":flags -lm", but that doesn't help. Looking at the code, it > seems the -lm flag is inserted in the wrong place, before the source > file. An easy change would be in line 147 of ob-C.el version > 8.3.4-634, moving "flags" to after the source file. This seems to > work, but I don't know if it would break anything else. > > A minimal working example, attached, includes the following snippet: > > #+BEGIN_SRC C :includes '( ) :flags -lm > int i=9; > printf("%d\n",(int)sqrt(i)); > #+END_SRC > > Note the issue disappears if constants are directly used rather than > variables, presumably because the function call is optimized away > entirely. > > Any thoughts are appreciated. > > Oz
Re: [O] org-babel C math.h issue
Anssi Saariwrites: > Nick Dokos writes: > >> You can test that that's the case: put your program into a file, say foo.c, >> and >> execute >> >> gcc -o foo.out -lm foo.c >> >> Does that give you undefined references? > > Does for me. gcc --version says > gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4 > > So is it gcc that has changed then? > Possibly. I use gcc (GCC) 5.3.1 20151207 (Red Hat 5.3.1-2) on Fedora 22. -- Nick
Re: [O] org-babel C math.h issue
Nick Dokoswrites: > You can test that that's the case: put your program into a file, say foo.c, > and > execute > > gcc -o foo.out -lm foo.c > > Does that give you undefined references? Does for me. gcc --version says gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4 So is it gcc that has changed then?
Re: [O] org-babel C math.h issue
Oz Ben-Amiwrites: > Hi, > > I'm a new org user, and I'm sorry if I'm missing something obvious. When > executing a C code snippet with > org-babel, that contains a math function, I get the famous "undefined > reference" errors. I tried adding > ":flags -lm", but that doesn't help. Looking at the code, it seems the -lm > flag is inserted in the wrong > place, before the source file. An easy change would be in line 147 of ob-C.el > version 8.3.4-634, moving > "flags" to after the source file. This seems to work, but I don't know if it > would break anything else. > > A minimal working example, attached, includes the following snippet: > > #+BEGIN_SRC C :includes '( ) :flags -lm > int i=9; > printf("%d\n",(int)sqrt(i)); > #+END_SRC > > Note the issue disappears if constants are directly used rather than > variables, presumably because the > function call is optimized away entirely. > > Any thoughts are appreciated. > I can't reproduce it either (with fairly recent emacs and bleeding-edge org-mode, but the code in question has not changed since 8.2.7 or so). The command that is executed looks like this: gcc -o /tmp/babel-212464kj/C-bin-21246R6L -lm /tmp/babel-212464kj/C-src-21246EwF.c and that should work fine for gcc: you don't need to have the -lm after the source file. You can test that that's the case: put your program into a file, say foo.c, and execute gcc -o foo.out -lm foo.c Does that give you undefined references? -- Nick
Re: [O] org-babel C math.h issue
I get the right answer on a Mac with org-mode version 8.2.10 Anssi Saari writes: > Oz Ben-Amiwrites: > >> A minimal working example, attached, includes the following snippet: >> >> #+BEGIN_SRC C :includes '( ) :flags -lm >> int i=9; >> printf("%d\n",(int)sqrt(i)); >> #+END_SRC > > I get the same problem, Ubuntu 14.04.3 LTS, GNU Emacs 24.3.1, Org-mode > version 8.2.10. So maybe it got fixed at some point since these versions > are about two years old? -- Professor John Kitchin Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 @johnkitchin http://kitchingroup.cheme.cmu.edu
Re: [O] org-babel C math.h issue
Oz Ben-Amiwrites: > A minimal working example, attached, includes the following snippet: > > #+BEGIN_SRC C :includes '( ) :flags -lm > int i=9; > printf("%d\n",(int)sqrt(i)); > #+END_SRC I get the same problem, Ubuntu 14.04.3 LTS, GNU Emacs 24.3.1, Org-mode version 8.2.10. So maybe it got fixed at some point since these versions are about two years old?
Re: [O] org-babel C math.h issue
Hello, Oz Ben-Amiwrites: > A minimal working example, attached, includes the following snippet: > > #+BEGIN_SRC C :includes '( ) :flags -lm > int i=9; > printf("%d\n",(int)sqrt(i)); > #+END_SRC FWIW, I get #+BEGIN_SRC C :includes '( ) :flags -lm int i=9; printf("%d\n",(int)sqrt(i)); #+END_SRC #+RESULTS: : 3 without any error. Regards, -- Nicolas Goaziou
[O] org-babel C math.h issue
Hi, I'm a new org user, and I'm sorry if I'm missing something obvious. When executing a C code snippet with org-babel, that contains a math function, I get the famous "undefined reference" errors. I tried adding ":flags -lm", but that doesn't help. Looking at the code, it seems the -lm flag is inserted in the wrong place, before the source file. An easy change would be in line 147 of ob-C.el version 8.3.4-634, moving "flags" to after the source file. This seems to work, but I don't know if it would break anything else. A minimal working example, attached, includes the following snippet: #+BEGIN_SRC C :includes '( ) :flags -lm int i=9; printf("%d\n",(int)sqrt(i)); #+END_SRC Note the issue disappears if constants are directly used rather than variables, presumably because the function call is optimized away entirely. Any thoughts are appreciated. Oz testC.org Description: Binary data