emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Richard Stanton <stanton@haas.berkeley.edu>
To: 'Eric Schulte' <eric.schulte@gmx.com>
Cc: "'emacs-orgmode@gnu.org'" <emacs-orgmode@gnu.org>
Subject: Re: Problems running C code in org-mode under Windows - SOLVED
Date: Thu, 29 Mar 2012 10:29:41 -0700	[thread overview]
Message-ID: <40C7B1BFC291ED4E9D10436D07736A334702EB9BA9@EXMAIL7.haas.uc.berkeley.edu> (raw)

> One problem - when using bash as the shell, when trying to execute the
> compiled file, it tries to execute the empty file, not the file that was just
> compiled (which has the same name, but the extension .exe, added by the
> compiler).
> 
> A partial solution to this is to append ".exe" to the name of the binary temp
> file if running under Windows. E.g., start org-babel-C-execute something
> like this:
> 
> (defun org-babel-C-execute (body params)
>   "This function should only be called by `org-babel-execute:C'
> or `org-babel-execute:C++'."
>   (let* ((tmp-src-file (org-babel-temp-file
> 			"C-src-"
> 			(cond
> 			 ((equal org-babel-c-variant 'c) ".c")
> 			 ((equal org-babel-c-variant 'cpp) ".cpp"))))
>          (tmp-bin-file (org-babel-temp-file
> 			"C-bin-"
> 			(if (equal system-type 'windows-nt) ".exe" "")))
> 
> [...]
> 
> I say "partial solution" because the output in the org file is *still* blank, but
> at least the program does get run this way...

Solved (approximately):

Here was the org file I was using:

#+begin_src C :includes <stdio.h>
    int a=2;
    int b=3;
    printf("%d\n", a+b);
#+end_src

This ran fine, but returned an error code of 2, which caused problems. When I added the extra line
 
   return(0);

at the end, it ran fine.

To allow for people who might be using the Cygwin bash shell in Emacs under Windows (which is recommended by many),  I do recommend making the change I suggested above, adding the following lines to ob-C.el:

>          (tmp-bin-file (org-babel-temp-file
> 			"C-bin-"
> 			(if (equal system-type 'windows-nt) ".exe" "")))
>

This prevents bash trying to run an (empty) file with no extension when the compiler has generated a file with a ".exe" extension. Otherwise, it looks like I now have this running fine. 

I also suggest not creating an empty binary file and just letting the compiler create it, unless this poses some risks I'm not seeing.

Thanks.

             reply	other threads:[~2012-03-29 17:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-29 17:29 Richard Stanton [this message]
  -- strict thread matches above, loose matches on Subject: below --
2012-03-29 17:41 Problems running C code in org-mode under Windows - SOLVED Richard Stanton
2012-03-29 19:18 ` Nick Dokos

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=40C7B1BFC291ED4E9D10436D07736A334702EB9BA9@EXMAIL7.haas.uc.berkeley.edu \
    --to=stanton@haas.berkeley.edu \
    --cc=emacs-orgmode@gnu.org \
    --cc=eric.schulte@gmx.com \
    /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).