Compiling C with Emacs on windows system - c

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

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?

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.

Related

How to use VC++ compiler in command line(Windows)?

I tried to use VC++ commandline, instead of MinGW compiler for windows system programming. I wrote a simple hello world program and tried to compile it, then i got this error message.
test2.c(1): fatal error C1083:'stdio.h': No such file or directory
I also added "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\bin\Hostx86\x86" directory (where c1.dll lives) to the System Envionment Variable(PATH).
How can I fix this issue? Other tutorials don't give much information about VC++. (A lot of MinGW compiler tutorial out there btw)
You should use visual studio command line if you want to compile or run program with the help of vc++ compiler.
Else all information related to setting environment variables ETC. resides in this MSDN document.

New to C: Compiling in Visual Studio Code... error: gcc not recognized?

I am very new to coding (trying to teach myself C). I have some experience with MatLab, but I understand it is very different from C. I have Windows 10 with the newest version of Visual Studio Code (VSC) with the Run Code Extension and git extension (not sure what git does, but VSC prompted me to install). I am now familiar with the text editor, but do not know how to compile/run my code (apologies if I'm butchering the terminology, again, a newby). One friend recommended I determine the location in which the desired text file is located, type "gcc filename.c", enter, type "./a.out", and then the program should run. (Said friend has a Mac and I supposed the execution commands are different?) Regardless, I encountered "Run Code" extension on my google adventures and follow the steps I've seen online, but I am still getting the error pictured.
I can see how this would be an especially basic question, but if anyone can offer assistance/advice, I would be extremely grateful!
Thanks, All.
Sandy
P.S. In case the image doesn't load/work for whatever reason, this is the error:
"'gcc' is not recognized as an internal or external command,
operable program or batch file."
Works perfect in Visual Code. You need the following:
C/C++ extension in Visual Code.
msys64 installed in C:\
Add path to msys64 bin folder in environment variable
Instructions:
https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools
Important the msys64 path doesn't contain spaces so don't install it in Program Files.
You can search "Edit system environment variables" from the windows button, press button Environment Variables, and then add it as a System variable "Path". For me its located in
C:\msys64\mingw64\bin
Hey,,,
It will work for you I believe
Blockquote
First: open the link: https://www.msys2.org/
Go to the installation and download the installer .. install the exe file.
search mingw in windows search(windows start)
there is an app named--> MSYS2 MinGW {64/32}-bit
run it as administrator
Found command shell ---> type: pacman -Syu
Will ask you for some installation click on Y
Then again go to the same app MSYS2 MinGW {64/32}-bit and run as admin
and write--> pacman -Ss gcc (in shell that opend after click)
after clicking there will be bunch of things and now you have to care about your windows arch.. if it is 64 bit then write---> pacman -S mingw-w64-x86_64-gcc (and if its 32 you will find something like that where 64 will replaced by 32 in bunch of commands that are showing in your shell)
after executing this command gcc will be installed in your system to check write: gcc --version || g++ --version
After that to install the debugger write pacman -S mingw-w64-x86_64-gdb
to check write: gdb --version
**Every thing got installed in your system now find the mingW file or mysys2 file on C drive(whereever you r mingW file got saved) go the the minGW 64 || 32 accroding to your operating system there is a bin folder click on that and copy the path inside the bin folder and save it in environment variables path **
Blockquote
Enjoy vs code
My recommendation
You are on Windows right? So you can just install Visual Studio IDE (follow this tutorial). Which is better than vs code. This tutorial is for C++ but it works for C as well. Because C++ uses the same compiler as C but with some more things(simple explanation).
The solution for your problem
You are getting the massage 'gcc' is not recognized as an internal or external command, operable program or batch file because you do not have the gcc compiler installed on your computer. But if you want to install it, you can follow this tutorial. But I highly recommend you use Visual Studio IDE which I mentioned above.
Type gcc --version in the command prompt to check whether the C compiler is installed in your machine.
If it is installed then try adding gcc to the environment variables using this link:
https://www.youtube.com/watch?v=qLh84CmdBJ0
If it is not installed then install it using instructions using this link:
https://www.guru99.com/c-gcc-install.html
and then add it to the environment variables using the link above.
If it is installed and not recognized by VSCode then try to run VSCode from CMD by typing code in CMD.

Debugging cross-compiled code: Linux->Windows

I'm cross-compiling a project from Linux to target Windows (using mingw). The output is a DLL and p-invoking into it from C# works, but debugging is very difficult. The build outputs a .o file, which can provide symbols to gdb, but basically all I can do there is break on exceptions and find the name of the function that was executing when the exception happened; not even the full stack trace. I can't debug with WinDbg because I don't have .pdb files.
This is an open source project set up to build on Linux; I believe their build process relies on several installed Linux packages to work.
Do I have any options here? Is there a utility that can convert .o files into .pdb? Or some program that can give me more information than gdb when debugging?
Try a IDE that support mingw. For example the open source Code::blocks.
Another possibility is to do it manually: compile it with debug symbols, start you application and attach the GDB debugger to it. It is also part of the MingW32 distribution. Then you can set your breakpoints and debug your application
But I guess using Code::Block is more comfortable
By the way, the GCC compiler does not generate pdb files because it is a propietary format
What xpol means is maybe: if you have a complete mingw installation then Code::blocks can use gdb to visualize a debugging session like it is done in Visual Studio or Eclipse. See chapter "Debugger" at http://www.codeblocks.org/features
You can generate a .pdb file using cv2pdb.exe from Visual D. This works even for programs not written in D if they were compiled with mingw. Once you've downloaded and installed Visual D cv2pdb.exe can be found at C:\Program Files (x86)\VisualD\cv2pdb\cv2pdb.exe.
You can run cv2pdb.exe against an executable like this:
cv2pdb.exe -n target.exe
This will produce a file called target.pdb. Assuming both target.pdb and target.exe are in the current director, you can then use windbg like this:
windbg -sflags 0x80030377 -y . -z target.dmp
In this case I'm also passing a minidump file as target.dmp. This can be omitted. The -sflags 0x80030377 option tells windbg to load target.pdb even though it thinks it doesn't match target.exe.
Note, that it can take windbg a very long time to load target.pdb. Just wait until it no longer says *BUSY* to the left of the command entry box.
Alternatively you can try DrMinGW.

Compiling C Source with Makefile in Windows

I'm trying to compile a downloaded program in Windows. The program is usually run in Linux, but is programmed to also run in Windows (the code has #if defined(_WIN32)'s in it, and claims to work with borland free tools). When I try to use make from the command line, it tells me "Incorrect command line argument: -C". In the makefile, there are many lines that say "make -C" followed by a directory name. Does this syntax not work in Windows? What is a correct way to do this? Is there any way to compile this for native use in Windows with this makefile?
Windows itself doesn't come with a make utility. Microsoft does have a 'make' utility that comes with their development tools (such as Visual Studio, the Platform SDK, or the Windows Driver Kit) but it's called nmake.
You probably need GNU make to process those makefiles. you can get a copy for Windows here:
http://unxutils.sourceforge.net/
However, if the makefile isn't written to be able to be run on Windows, it'll probably not work well. You'll also need to make sure you have whatever other development tools the makefile calls upon (maybe the Borland compiler or GCC), and there may be other configuration that needs to be done specific to the project you want to build. It's probably not a matter of just having the correct make utility.
-C is "change working directory" only for the gmake command (from the GNU package). You should take a look in the manual for your Make-Utility and see, wheather it supports something äquivalent.
Peter
Are you using cygwin?
Are there any instructions for installing on windows(perhaphs in a README file)?

How to compile a C program?

I haven't done C in a long time. I'd like to compile this program, but I have no idea how to proceed. It seems like the makefile refers to GCC a lot and I've never used GCC.
I just want an executable that will run on windows.
You may need to install either cygwin or mingw, which are UNIX-like environments for Windows.
http://www.mingw.org/
http://www.cygwin.com/
When downloading/installing either cygwin or mingw, you will have the option of downloading and installing some optional features; you will need the following:
gcc (try version 2.x first, not 3.x)
binutils
GNU make (or gmake)
If it requires gcc and you want it to run on Windows, you could download Cygwin.
That's basically an emulator for GNU/Linux type stuff for Windows. It works with an emulation DLL.
http://www.cygwin.com/
In order to compile this program you need a C compiler. It does not have to be gcc, although you are already given a makefile set up to use gcc. The simplest thing for you to do would be the following:
Install cygwin
Open the cygwin command prompt
go into the directory where you have your makefile
type 'make'
That should compile your program
If you are not comfortable with using command line tools then you can download the free version of MS Visual Studio and import the source files into a new Visual Studio project. This way you would not need to install cygwin and use gcc, but you would need to know how to create projects and run programs in Visual Studio.
You almost certainly don't need all of cygwin to compile using gcc. There are plenty of standalone gcc clones for Windows, like gcw.
If it's reasonably portable C code (I haven't looked at it), then you may be able to just ignore the included Makefile and feed the source into whatever compiler you do want to use. What happens when you try that?
Dev-C++ provides a simple but nice IDE which uses the Mingw gcc compiler and provides Makefile support. Here are the steps I used to build the above code using Dev-C++ (i.e. this is a "how-to")
After downloading the source zip from NIST, I
downloaded and installed the Dev-C++ 5 beta 9 release
created a new empty project
added all the .c files from sts-2.0\src
Then under Project Options
added -lm in the Linker column under Parameters
added sts-2.0\include to the Include Directories in Directories
set the Executable and Object directories to the obj directory under the Build Options
and then hit OK to close the dialog. Go to Execute > Compile and let it whirl. A minute later, you can find the executable in the sts-2.0\obj directory.
First, there is little chance that a program with only makefiles will build with visual studio, if only because visual studio is not a good C compiler from a standard POV (the math functions in particular are very poorly supported on MS compilers). It may be possible, but it won't be easy, specially if you are not familiar with C. You should really stick to the makefiles instead of trying to import the code in your own IDE - this kind of scienfitic code is clearly meant to be compiled from the command line. It is a test suite, so trying things randomly is NOT a good idea.
You should use mingw + msys to install it: mingw will give you the compilers (gcc, etc...) and msys the shell for the make file to run correctly. Contrary to one other poster, I would advise you against using gcc 2 - I don't see any point in that. I routinely use gcc 3 (and even 4) on windows to build scientific code, it works well when the code is unix-like (which is the standard platform for this kind of code).

Resources