From mboxrd@z Thu Jan 1 00:00:00 1970 From: Oz Ben-Ami Subject: Re: org-babel C math.h issue Date: Fri, 11 Mar 2016 14:14:21 -0500 Message-ID: References: <56E313E9.4010703@free.fr> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=047d7b15acabcc24e6052dcabe10 Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:36575) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aeSWU-0001u3-4h for emacs-orgmode@gnu.org; Fri, 11 Mar 2016 14:14:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aeSWS-0002BF-KF for emacs-orgmode@gnu.org; Fri, 11 Mar 2016 14:14:54 -0500 Received: from mail-pa0-x231.google.com ([2607:f8b0:400e:c03::231]:35772) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aeSWS-0002Ao-8i for emacs-orgmode@gnu.org; Fri, 11 Mar 2016 14:14:52 -0500 Received: by mail-pa0-x231.google.com with SMTP id td3so79679500pab.2 for ; Fri, 11 Mar 2016 11:14:51 -0800 (PST) In-Reply-To: <56E313E9.4010703@free.fr> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Thierry Banel Cc: emacs-orgmode@gnu.org --047d7b15acabcc24e6052dcabe10 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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 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 '( ) :libs -lm > int i=3D9; > 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 =C3=A9crit : > > 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=3D9; > > 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 > > > --047d7b15acabcc24e6052dcabe10 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
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 fi= le name.

As far as a patch, I can try but this would be my fi= rst time really playing with org-mode code. As far as I can see, I would ne= ed to add the following lines in ob-C.el:
L133: (libs (cdr (assoc :libs = params)))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (libs (mapcon= cat 'identity
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2= =A0 =C2=A0=C2=A0 (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, Thier= ry 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:

=C2=A0 =C2=A0 (defadvice org-babel-eval (before xxx (cmd body))
=C2=A0 =C2=A0 =C2=A0 ""
=C2=A0 =C2=A0 =C2=A0 (message "org-babel-eval : %s" cmd))
=C2=A0 =C2=A0 (ad-activate 'org-babel-eval)

The actual compilation command will be displayed in the *Messages* buffer.<= br>
With your example, we get

=C2=A0 =C2=A0gcc -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:

=C2=A0 #+BEGIN_SRC C :includes '(<math.h> <stdio.h>) :libs = -lm
=C2=A0 int i=3D9;
=C2=A0 printf("%d\n",(int)sqrt(i));
=C2=A0 #+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 =C3=A9crit :
> 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 t= ried
> adding ":flags -lm", but that doesn't help. Looking at t= he 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 see= ms 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=3D9;
> 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



--047d7b15acabcc24e6052dcabe10--