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 w开发者_Go百科ith 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, CygwinI 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?
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)
You don't need gcc or cygwin to compile C code on Windows.
I use the compiler, make utility, linker, and other tools that come with the (free) Microsoft Visual-C++ Express edition and the (free) Windows SDK, within emacs 23.2.
Some tips for you:
- use this in your .emacs file:
(eval-after-load "compile"
'(progn
(setq compilation-scroll-output "first-error")
(setq-default compile-command "nmake ")))
- Add appropriate error message regexii to the compilation error regexp list. Like this:
(mapcar
(lambda (x)
(add-to-list 'compilation-error-regexp-alist-alist x))
(list
;; Microsoft C/C++:
;; keyboard.c(537) : warning C4005: 'min' : macro redefinition
;; d:\tmp\test.c(23) : error C2143: syntax error : missing ';' before 'if'
;; .\cppcli1.cpp(36): error C2059: syntax error : 'public'
;; e:\projects\myce40\tok.h(85) : error C2236: unexpected 'class' '$S1'
;; myc.cpp(14) : error C3149: 'class System::String' : illegal use of managed type 'String'; did you forget a '*'?
;; ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) ?\: \\(error\\|warning\\) C[0-9]+:" 1 3)
'(msvc "^[ \t]*\\([A-Za-z0-9\\.][^(]*\\.\\(cpp\\|c\\|h\\)\\)(\\([0-9]+\\)) *: +\\(error\\|fatal error\\|warning\\) C[0-9]+:" 1 3)
))
(setq compilation-error-regexp-alist
(mapcar 'car compilation-error-regexp-alist-alist))
I am new to Emacs, Cygwin Bash, and general C terminology so I found some of the other explanations in this thread and others a bit confusing for me to follow. I eventually found a solution on my own. For those who are in a similar situation, I hope these simple instructions help. If anyone more advanced than I has additions to add I can update this post.
- Install Cygwin.
- On the setup screen go into the "devel" folder. Select "gcc-core" if it has not already been downloaded into your Cygwin setup. Finish installation.
- Install Emacs.
- Find the .emacs file (the Emacs configuration file, located for me in the main Emacs folder) and add in the following:
(setq explicit-shell-file-name "{Path to Cygwin folder}\\Cygwin.bat")
(setq shell-file-name "bash")
(setenv "SHELL" shell-file-name)
Set your own path to Cygwin.bat where indicated. Note that you must use Cygwin.bat. It does not appear to work if you go straight to bash.exe. Also, you may need to use double backslashes to get the escape characters to work or Emacs may complain at startup.
Now you can test if it works by going into Emacs and typing M-x shell
. It should bring up the Cygwin bash shell in a new buffer.
As an example, compile and execute the program "foo.c" through Cygwin Bash:
- Compile:
gcc foo.c -o foo.exe
. This builds the file and makes an executable with the same name. - Execute:
./foo.exe
. The output should be shown in Emacs shell buffer.
This has worked very well for me and I hope this will be a simple alternative for those that don't want to go through configuring Visual Studio or other solutions.
精彩评论