Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling C with Emacs on windows system

I have a little bit of C programming experience from school, but it was all Unix. I want to compile some C with Emacs, using Emacs as a second IDE.

What is the easiest way to go about doing this? I downloaded Cygwin with no successful compile of a helloWorld.c in Emacs. Even opening the cygwin command prompt (shell or whatever its called), it doesn't recognize gcc as a command.

Error
'make' is not recognized as an internal or external command, operable program or batch file.

I need a compiler that works on Windows, is free (or comes with visual studio), and can be used with Emacs. What to type into Emacs after the M-x compile command would be nice to know too. I looked at MinGW, but downloading it is a chore.

I hope the question makes sense. I often get lost in the open source world.

Environment
Windows XP, Visual Studio 2010, Emacs 23.2.1, Windows 7.1 SDK installed, Cygwin

like image 435
P.Brian.Mackey Avatar asked Dec 29 '10 17:12

P.Brian.Mackey


2 Answers

I use Visual Studio as a compiler with emacs as an editor.

Just install Visual Studio C++ 2010 Express Edition. Then what I do is write an nmake Makefile and invoke nmake from the Visual Studio Command Prompt (accessible from the Programs menu). This works fine for smaller projects.

See http://msdn.microsoft.com/en-us/library/f35ctcxw.aspx for more details.

For larger projects you can create a solution in Visual Studio and just use emacs as an editor. You can also invoke msbuild from the command prompt to build the solution.

Also, the visual studio command prompt just invokes vcvars.bat (or something like it) to set up the necessary environment. I guess you might be able to modify the emacs shell to point to an instance of cmd that has run this bat file on startup?

like image 114
Brian Coleman Avatar answered Oct 06 '22 00:10

Brian Coleman


Anything of this sort you attempt to do on windows is going to be a "chore" because you are trying to put together components from distinct sources. If you want easy, use an IDE and compiler packaged together, such as the Visual Studio freebie edition, or Code Blocks, or Dev C++, etc.

My understanding is that the M-x compile prompt is looking for a shell command, in the path of the shell that emacs runs which you can of course set somewhere. Depending on what build of emacs you have for windows, that might by default by the windows shell, or it could be bash under MINGW or cygwin.

To get gcc and make and such under cygwin you have to select these from the cygwin packages to install (using the cygwin installer efficiently is an an obscure skill in itself). Cygwin's gcc will by default compile things to depend on the cygwin dll, but you can also make mingw-style windows executables with the -mno-cygwin flag to gcc or by running mingw's gcc rather than cygwin's.

Presumably emacs could even launch the Visual Studio compiler if you figure out an appropriate command line for that, or its make utility, or you could run it from gnu make. Issues you are likely to run into when mixing and matching are windows vs unix paths, having your executable path include the necessary tools, and the likelihood that a foreign compiler may format errors in a way that the IDE won't parse to make them clickable. All of these things can be worked around (for example, during one phase of a project I had a sed script that reformatted GCC-cross errors to make them clickable in Visual Studio's compiler errors window)

like image 37
Chris Stratton Avatar answered Oct 06 '22 01:10

Chris Stratton