Compiling C with Emacs on Windows

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

What is the easiest way to do this? I downloaded Cygwin without successfully compiling helloWorld.c in Emacs. Even opening the cygwin command line (shell or any other name), it does not recognize gcc as a command.

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

I need a compiler that runs on Windows, is free (or comes with a visual studio), and can be used with Emacs. What to enter in Emacs after the Mx compile command would be good to know. I looked at MinGW, but downloading is the job.

Hope this question makes sense. I am often lost in the open source world.

Wednesday
Windows XP, Visual Studio 2010, Emacs 23.2.1, Windows 7.1 SDK, Cygwin

+4
source share
4 answers

I am using Visual Studio as a compiler with emacs as an editor.

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

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

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

In addition, the visual studio command line command only calls vcvars.bat (or something like that) to set up the required environment. I think you could change the emacs shell to point to the cmd instance that ran this bat file at startup?

+4
source

Everything that you try to do on windows will be "complicated" because you are trying to assemble components from different sources. If you want it easy, use the IDE and compiler, packaged together, for example, releases of Freebie Visual Studio or Code Blocks or Dev C ++, etc.

I understand that the Mx compilation prompt is looking for a shell command on the path to the shell that emacs runs, which of course you can install somewhere. Depending on which build of emacs you have for windows, which by default can be installed by the Windows shell, or it can be bash under MINGW or cygwin.

To get gcc and make, etc. in cygwin, you must select them from the cygwin packages to install (using the cygwin installer effectively is an obscure skill in itself). Cygwin gcc will compile things dependent on cygwin dll by default, but you can also create mingw-style window executables with the -mno-cygwin flag to gcc or run mingw gcc instead of cygwin.

Presumably emacs can even run the Visual Studio compiler if you select the appropriate command line for this or its make utility, or you can run it from gnu make. The problems you are likely to encounter when mixing and matching are the windows vs unix file paths, with your executable path including the necessary tools and the likelihood that the external compiler may format the errors in such a way that the IDE will not analyze, to make them interactive, All this can be circumvented (for example, at one stage of the project I had a sed script that reformatted GCC-cross errors to make them accessible in the error window of the Visual Studio compiler)

+3
source

You do not need gcc or cygwin to compile C code on Windows.

I use the compiler, create a utility, linker and other tools that come with the free version of Microsoft Visual C ++ Express and the (free) Windows SDK in 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 ")))
 (eval-after-load "compile" '(progn (setq compilation-scroll-output "first-error") (setq-default compile-command "nmake "))) 

  • Add the appropriate regexii error message to the list of compilation regular expressions. 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)) 
+3
source

I am new to Emacs, Cygwin Bash, and the general terminology of C, so I found some other explanations in this thread, while others are a little confusing for me. In the end, I found a solution myself. For those in a similar situation, I hope these simple instructions help. If someone is more advanced than I have add-ons, I can update this post.

  • Install Cygwin.
  • On the settings screen, go to the "devel" folder. Select "gcc-core" if it has not already been loaded into the Cygwin setup. Complete the installation.
  • Install Emacs.
  • Find the .emacs file (the Emacs configuration file located for me in the main Emacs folder) and add 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. Please note that you must use Cygwin.bat. It does not work if you go to bash.exe. Alternatively, you may need to use a double backslash to make escape characters work, or Emacs may complain at startup.

Now you can check if it works by going to Emacs and typing Mx shell . It should open the Cygwin bash shell in a new buffer.

As an example, compile and execute the program "foo.c" using Cygwin Bash:

  • Compile: gcc foo.c -o foo.exe . This creates a file and makes an executable with the same name.
  • Run: ./foo.exe . The output should be displayed in the Emacs shell buffer.

This worked very well for me, and I hope that it will be a simple alternative for those who do not want to go through the setup of Visual Studio or other solutions.

+2
source

Source: https://habr.com/ru/post/1333708/


All Articles