emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Re: Problems running C code in org-mode under Windows - SOLVED
@ 2012-03-29 17:29 Richard Stanton
  0 siblings, 0 replies; 3+ messages in thread
From: Richard Stanton @ 2012-03-29 17:29 UTC (permalink / raw)
  To: 'Eric Schulte'; +Cc: 'emacs-orgmode@gnu.org'

> 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.

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

* Re: Problems running C code in org-mode under Windows - SOLVED
  2012-03-29 17:41 Richard Stanton
@ 2012-03-29 19:18 ` Nick Dokos
  0 siblings, 0 replies; 3+ messages in thread
From: Nick Dokos @ 2012-03-29 19:18 UTC (permalink / raw)
  To: Richard Stanton
  Cc: nicholas.dokos, 'emacs-orgmode@gnu.org', 'Eric Schulte'

Richard Stanton <stanton@haas.berkeley.edu> wrote:

> > 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.
> 
> One more quick follow-up: It seems that I need to add a "return(0);"
> line to my main routine to get any C examples to work under Windows,
> whereas I don't need to add such a line on my Mac. I just tried the
> cocktail.c example from your 2012 Journal of Statistical Software
> article, and this works on my Mac, but fails on my PC (even with my
> edits to ob-C.el) unless I add "return(0);" at the end of the main
> code block.
> 
> It would be nice for Windows users to be able to run others' examples
> without having to edit them first, so what do you think of the idea of
> having ob-C.el append a "return(0);" line to the main file before
> passing it to the compiler?
> 
> 

AFAIK, dropping off the end of main() without a return or call to exit()
is supposed to be equivalent to having a "return 0;" there. Assuming
that that is true, my $0.02 would be to leave it alone: there are too
many stupid bugs out there for org to fix. In this case, the compiler
(btw, which compiler is it?) should be fixed, not org. In addition,
there are not too many people that run this particular combination of
software (you are probably the only one), and now that you have found a
reasonable work-around for the problem, I'd encourage you to just add a
question-and-answer to the FAQ: imo, that's enough.

Nick

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

* Re: Problems running C code in org-mode under Windows - SOLVED
@ 2012-03-29 17:41 Richard Stanton
  2012-03-29 19:18 ` Nick Dokos
  0 siblings, 1 reply; 3+ messages in thread
From: Richard Stanton @ 2012-03-29 17:41 UTC (permalink / raw)
  To: 'Eric Schulte'; +Cc: 'emacs-orgmode@gnu.org'

> 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.

One more quick follow-up: It seems that I need to add a "return(0);" line to my main routine to get any C examples to work under Windows, whereas I don't need to add such a line on my Mac. I just tried the cocktail.c example from your 2012 Journal of Statistical Software article, and this works on my Mac, but fails on my PC (even with my edits to ob-C.el) unless I add "return(0);" at the end of the main code block. 

It would be nice for Windows users to be able to run others' examples without having to edit them first, so what do you think of the idea of having  ob-C.el append a "return(0);" line to the main file before passing it to the compiler? 

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

end of thread, other threads:[~2012-03-29 19:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-29 17:29 Problems running C code in org-mode under Windows - SOLVED Richard Stanton
2012-03-29 17:41 Richard Stanton
2012-03-29 19:18 ` Nick Dokos

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).