emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Oz Ben-Ami <ozzieba@gmail.com>
To: Thierry Banel <tbanelwebmin@free.fr>
Cc: emacs-orgmode@gnu.org
Subject: Re: org-babel C math.h issue
Date: Fri, 11 Mar 2016 14:14:21 -0500	[thread overview]
Message-ID: <CABYMxKxaExgmjbbv3tCVDWHNJgtT4hw0tg1rU1o-huMmi3ibGg@mail.gmail.com> (raw)
In-Reply-To: <56E313E9.4010703@free.fr>

[-- 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 --]

  reply	other threads:[~2016-03-11 19:14 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CABYMxKxaExgmjbbv3tCVDWHNJgtT4hw0tg1rU1o-huMmi3ibGg@mail.gmail.com \
    --to=ozzieba@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=tbanelwebmin@free.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).