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

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.

Related

"Availability status: error" for working C programs

I recently learned about Visual Studio Code & Intellisense and am currently writing code on that and trying to compile it using Cygwin. However, I'm never able to do it.
I have added C:\cygwin64\binand%systemdrive%:\cygwin\bin` to my system environment variables in settings. I'm doing this on a Windows 11 computer. But every time I try to compile a program, like:
gcc HelloWorld.c
I get:
cc1: fatal error: HelloWorld.c: No such file or directory
compilation terminated.
In file explorer, all the programs I'm trying to compile say "Availability status: error". Specifically, any programs (regardless of language) I have downloaded onto my computer hard drive are apparently unavailable. What does this mean?
I tried using MSYS Mingw-w64 but the same problem happens. I thought that I might not have included the "gcc-core" compiler file when I downloaded it, so i did that again with the file but nothing. I typed "gcc --version" and "gdb -- version" to check and debug but the same messages pop up, both for Cygwin and Mingw.
I tried using other text editors (notepad and vim) to type, but they didn't have any effect. I thought they might create compileable, "available" files on file explorer.

Why can Visual Studio 2017 Developer Command Promt only open "stdio.h" in Administrator mode?

I am trying to do simulation using Delmia. This requires a functioning C compiler. When i test my C compiler in Delmia, which tries to compile a very simple source file that includes stdio.h, I get the error "Cannot open include file: 'stdio.h': No such file or directory":
Compiling and linking the model (Visual C++).
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.6.2
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'
Guessed compiler version: 1913
"Testing 32-bit compilation"
dsmodel.c
dsmodel.c(1): fatal error C1083: Cannot open include file: 'stdio.h': No such file or directory
Error generating Dymosim.
It seems to be entirely the same issue as:
Visual Studio 2017 for Dymola cannot open stdio.h
His solution is not descriptive enough for me to solve my issue, as his "IT dept" apparently solved it for him, but he mentions allowing access to regedit which I do not understand the reason for in this context.
I have tried to recreate the error that Delmia generates by compiling dsmodel.c with VS2017 Developer Command Promt. If I run the command prompt in Administrator it compiles fine, if I do not, I get the samme error as Delmia outputs. Why is this?
If I find a solution such that I can compile it with command prompt without being in Administrator mode, then should Delmia not also be able to? How can i achieve this?
Some additional information: I have installed Visual Studio not on my main drive, and the Windows SDK is in program files on my main drive. Does this have any relevance?
Edit: For anyone else with this issue in Delmia, I would suggest simply installing another compatible compiler as a work-around. I installed MinGW which works well, even though this additional compiler takes up 500 MB. Remember that it has to be 64-bit. I used the "MingW-W64-builds" from http://mingw-w64.org/doku.php/download

Can't compile C on windows 7 x64

I have visual studio 2012 ultimate and mingw installed on my pc.
However, when I try to compile any program (I have only tried a simple hello world program)
e.g when I use the visual studio cl compiler or the gcc compiler I get the error :
cl is not recognized as an internal or external command, operable program or batch file.
and similar for mingw-gcc.
I have visual studio installed in the default location. I have installed mingw in D:\MinGw but I have added it to the path variable. Also I have installed CodeBlocks after getting frustrated with this and my program builds and runs easily when I try that from inside CodeBlocks. I can also compile my program from inside Visual Studio by clicking on the build and run buttons.
What do I need to do to compile and run programs from the commandline in windows 7?
Instead of running a "regular" command prompt, run the "Visual Studio command prompt". You can find a link to it in the start menu.
It's essentially the same, but it sets up for you all the environment variables you need for all the VS tools to work from the command line.
Sounds like you still have a path problem. The mingw-gcc path for example would need to be to the bin directory:
D:\MinGW\bin
in your case. Then you should be able to run gcc.

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

Resources