emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* org-babel C math.h issue
@ 2016-03-06 22:44 Oz Ben-Ami
  2016-03-10  9:43 ` Nicolas Goaziou
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Oz Ben-Ami @ 2016-03-06 22:44 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 884 bytes --]

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 '(<math.h> <stdio.h>) :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

[-- Attachment #1.2: Type: text/html, Size: 1049 bytes --]

[-- Attachment #2: testC.org --]
[-- Type: application/octet-stream, Size: 267 bytes --]

#+BEGIN_SRC elisp
(custom-set-variables
 '(org-babel-load-languages
   (quote
    ((emacs-lisp . t)
     (C . t)))))
#+END_SRC

#+RESULTS:


#+BEGIN_SRC C :includes '(<math.h> <stdio.h>) :flags -lm
int i=9;
printf("%d\n",(int)sqrt(pow(i,i)));
#+END_SRC

#+RESULTS:



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: org-babel C math.h issue
  2016-03-06 22:44 org-babel C math.h issue Oz Ben-Ami
@ 2016-03-10  9:43 ` Nicolas Goaziou
  2016-03-10 14:06 ` Anssi Saari
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 13+ messages in thread
From: Nicolas Goaziou @ 2016-03-10  9:43 UTC (permalink / raw)
  To: Oz Ben-Ami; +Cc: emacs-orgmode

Hello,

Oz Ben-Ami <ozzieba@gmail.com> writes:

> A minimal working example, attached, includes the following snippet:
>
> #+BEGIN_SRC C :includes '(<math.h> <stdio.h>) :flags -lm
> int i=9;
> printf("%d\n",(int)sqrt(i));
> #+END_SRC

FWIW, I get

  #+BEGIN_SRC C :includes '(<math.h> <stdio.h>) :flags -lm
  int i=9;
  printf("%d\n",(int)sqrt(i));
  #+END_SRC

  #+RESULTS:
  : 3

without any error.


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: org-babel C math.h issue
  2016-03-06 22:44 org-babel C math.h issue Oz Ben-Ami
  2016-03-10  9:43 ` Nicolas Goaziou
@ 2016-03-10 14:06 ` Anssi Saari
  2016-03-10 15:20   ` John Kitchin
  2016-03-10 21:19 ` Nick Dokos
  2016-03-11 18:52 ` Thierry Banel
  3 siblings, 1 reply; 13+ messages in thread
From: Anssi Saari @ 2016-03-10 14:06 UTC (permalink / raw)
  To: emacs-orgmode

Oz Ben-Ami <ozzieba@gmail.com> writes:

> A minimal working example, attached, includes the following snippet:
>
> #+BEGIN_SRC C :includes '(<math.h> <stdio.h>) :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?

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: org-babel C math.h issue
  2016-03-10 14:06 ` Anssi Saari
@ 2016-03-10 15:20   ` John Kitchin
  0 siblings, 0 replies; 13+ messages in thread
From: John Kitchin @ 2016-03-10 15:20 UTC (permalink / raw)
  To: Anssi Saari; +Cc: emacs-orgmode

I get the right answer on a Mac with org-mode version 8.2.10


Anssi Saari writes:

> Oz Ben-Ami <ozzieba@gmail.com> writes:
>
>> A minimal working example, attached, includes the following snippet:
>>
>> #+BEGIN_SRC C :includes '(<math.h> <stdio.h>) :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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: org-babel C math.h issue
  2016-03-06 22:44 org-babel C math.h issue Oz Ben-Ami
  2016-03-10  9:43 ` Nicolas Goaziou
  2016-03-10 14:06 ` Anssi Saari
@ 2016-03-10 21:19 ` Nick Dokos
  2016-03-11  6:44   ` Anssi Saari
  2016-03-11 18:52 ` Thierry Banel
  3 siblings, 1 reply; 13+ messages in thread
From: Nick Dokos @ 2016-03-10 21:19 UTC (permalink / raw)
  To: emacs-orgmode

Oz Ben-Ami <ozzieba@gmail.com> writes:

> 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 '(<math.h> <stdio.h>) :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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: org-babel C math.h issue
  2016-03-10 21:19 ` Nick Dokos
@ 2016-03-11  6:44   ` Anssi Saari
  2016-03-11 15:36     ` Nick Dokos
  0 siblings, 1 reply; 13+ messages in thread
From: Anssi Saari @ 2016-03-11  6:44 UTC (permalink / raw)
  To: emacs-orgmode

Nick Dokos <ndokos@gmail.com> 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?

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: org-babel C math.h issue
  2016-03-11  6:44   ` Anssi Saari
@ 2016-03-11 15:36     ` Nick Dokos
  0 siblings, 0 replies; 13+ messages in thread
From: Nick Dokos @ 2016-03-11 15:36 UTC (permalink / raw)
  To: emacs-orgmode

Anssi Saari <as@sci.fi> writes:

> Nick Dokos <ndokos@gmail.com> 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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: org-babel C math.h issue
  2016-03-06 22:44 org-babel C math.h issue Oz Ben-Ami
                   ` (2 preceding siblings ...)
  2016-03-10 21:19 ` Nick Dokos
@ 2016-03-11 18:52 ` Thierry Banel
  2016-03-11 19:14   ` Oz Ben-Ami
  3 siblings, 1 reply; 13+ messages in thread
From: Thierry Banel @ 2016-03-11 18:52 UTC (permalink / raw)
  To: emacs-orgmode

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 '(<math.h> <stdio.h>) :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 '(<math.h> <stdio.h>) :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

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: org-babel C math.h issue
  2016-03-11 18:52 ` Thierry Banel
@ 2016-03-11 19:14   ` Oz Ben-Ami
  2016-03-11 23:14     ` Thierry Banel
  2016-03-20 14:03     ` [PATCH] " Thierry Banel
  0 siblings, 2 replies; 13+ messages in thread
From: Oz Ben-Ami @ 2016-03-11 19:14 UTC (permalink / raw)
  To: Thierry Banel; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 3003 bytes --]

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 Banel <tbanelwebmin@free.fr> wrote:

> 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 '(<math.h> <stdio.h>) :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 '(<math.h> <stdio.h>) :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
>
>
>

[-- Attachment #2: Type: text/html, Size: 4016 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: org-babel C math.h issue
  2016-03-11 19:14   ` Oz Ben-Ami
@ 2016-03-11 23:14     ` Thierry Banel
  2016-03-20 14:03     ` [PATCH] " Thierry Banel
  1 sibling, 0 replies; 13+ messages in thread
From: Thierry Banel @ 2016-03-11 23:14 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/html, Size: 6583 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] org-babel C math.h issue
  2016-03-11 19:14   ` Oz Ben-Ami
  2016-03-11 23:14     ` Thierry Banel
@ 2016-03-20 14:03     ` Thierry Banel
  2016-03-25 23:42       ` Nicolas Goaziou
  1 sibling, 1 reply; 13+ messages in thread
From: Thierry Banel @ 2016-03-20 14:03 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 765 bytes --]

Attached is Oz's patch adding optional :libs support to Babel C++
Thanks Oz.
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
>


[-- Attachment #2: 0001-Add-libs-to-babel-C.patch --]
[-- Type: text/x-diff, Size: 1543 bytes --]

From 69661d4406c51f678dad6f14aaa8f3447c398825 Mon Sep 17 00:00:00 2001
From: Thierry Banel <tbanelwebmin@free.fr>
Date: Sun, 20 Mar 2016 14:50:17 +0100
Subject: [PATCH] Add :libs to babel C++

* lisp/ob-C.el (org-babel-C-execute): add support for :libs header
  parameter to specify libraries to link with.
---
 lisp/ob-C.el | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/lisp/ob-C.el b/lisp/ob-C.el
index dd34b0b..d5855ab 100644
--- a/lisp/ob-C.el
+++ b/lisp/ob-C.el
@@ -130,6 +130,12 @@ or `org-babel-execute:C++' or `org-babel-execute:D'."
 	 (flags (cdr (assoc :flags params)))
 	 (flags (mapconcat 'identity
 			   (if (listp flags) flags (list flags)) " "))
+	 (libs (org-babel-read
+		(or (cdr (assoc :libs params))
+		    (org-entry-get nil "libs" t))
+		nil))
+	 (libs (mapconcat 'identity
+			   (if (listp libs) libs (list libs)) " "))
 	 (full-body
 	  (case org-babel-c-variant
 	    (c   (org-babel-C-expand-C   body params))
@@ -139,13 +145,15 @@ or `org-babel-execute:C++' or `org-babel-execute:D'."
     (case org-babel-c-variant
       ((c cpp)
        (org-babel-eval
-	(format "%s -o %s %s %s"
+	(format "%s -o %s %s %s %s"
 		(case org-babel-c-variant
 		 (c   org-babel-C-compiler)
 		 (cpp org-babel-C++-compiler))
 		(org-babel-process-file-name tmp-bin-file)
 		flags
-		(org-babel-process-file-name tmp-src-file)) ""))
+		(org-babel-process-file-name tmp-src-file)
+		libs)
+	""))
       (d nil)) ;; no separate compilation for D
     (let ((results
 	   (org-babel-eval
-- 
2.1.4


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH] org-babel C math.h issue
  2016-03-20 14:03     ` [PATCH] " Thierry Banel
@ 2016-03-25 23:42       ` Nicolas Goaziou
  2016-03-26 17:08         ` Thierry Banel
  0 siblings, 1 reply; 13+ messages in thread
From: Nicolas Goaziou @ 2016-03-25 23:42 UTC (permalink / raw)
  To: Thierry Banel; +Cc: emacs-orgmode

Hello,

Thierry Banel <tbanelwebmin@free.fr> writes:

> Attached is Oz's patch adding optional :libs support to Babel C++

Applied. Thank you.

Would you (or the OP) mind documenting the feature at
<http://orgmode.org/worg/org-contrib/babel/languages.html>?

Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] org-babel C math.h issue
  2016-03-25 23:42       ` Nicolas Goaziou
@ 2016-03-26 17:08         ` Thierry Banel
  0 siblings, 0 replies; 13+ messages in thread
From: Thierry Banel @ 2016-03-26 17:08 UTC (permalink / raw)
  To: emacs-orgmode

Done.

Le 26/03/2016 00:42, Nicolas Goaziou a écrit :
> Applied. Thank you.
>
> Would you (or the OP) mind documenting the feature at
> <http://orgmode.org/worg/org-contrib/babel/languages.html>?
>

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2016-03-26 17:09 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-06 22:44 org-babel C math.h issue Oz Ben-Ami
2016-03-10  9:43 ` Nicolas Goaziou
2016-03-10 14:06 ` Anssi Saari
2016-03-10 15:20   ` John Kitchin
2016-03-10 21:19 ` Nick Dokos
2016-03-11  6:44   ` Anssi Saari
2016-03-11 15:36     ` Nick Dokos
2016-03-11 18:52 ` Thierry Banel
2016-03-11 19:14   ` Oz Ben-Ami
2016-03-11 23:14     ` Thierry Banel
2016-03-20 14:03     ` [PATCH] " Thierry Banel
2016-03-25 23:42       ` Nicolas Goaziou
2016-03-26 17:08         ` Thierry Banel

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).