MinGW and Eclipse on Windows - generating assembly output - c

I recently installed MinGW to play around with (non-AVR-specific) C development on Windows. I'd like to see the assembly generated by GCC along with the .o file; I've played around with changing some of the command line flags under "C/C++ build" and "Properties" for the test project I'm using, but I can't seem to alter the build behavior. Is there a straightforward way to do this? Thanks!

The way to do it is to add -Wa,-aln=output.s to the "Command" box under Properties > C/C++ Build > Settings > Tool Settings > GCC C Compiler.

Bitrex was on the right path but a better way to do is under "Properties > C/C++ Build > Settings > Tool Settings > GCC C Compiler > Miscellaneous", append -Wa,-aln='${CWD}\\${InputFileBaseName}.cod' (take notice of the single quotes) to the end of "Other flags". This will not only generate the assembly, but seperate them by the source file.
With Bitrex's way, if you have multiple source files, each assembly output will be overwritten by the next source file, because he forces them all to be named "output.s".
Note: Use "GCC C++ Compiler" if your compiling C++ code.

Related

"cannot open source file "vcruntime_string.h" (dependency of "string.h")" in VS Code

I am trying to learn C programming. And I am using VS Code for running almost everything.
But none of my C Codes execute. The c_cpp_properties.json is configured with the MinGW header files library path included in the "msvc-x64" section. It says "cannot open source file "vcruntime_string.h" (dependency of "string.h")" all the time.
First of all, it's weird that it is not letting you compile by not finding string.h dependency. I suggest to try the following:
Try compiling your files either with gcc or g++ (depending on the language you're using) directly from the terminal, to see if you get the "a.out" file.
Ex: Open your terminal. Find your .c files (if you're programming in C) gcc *.c then ./a.out
Try using CodeBlocks, Build and Run to see if you get the same error.
If 1 or 2 worked, it means your VS software didn't like something when you made the installation process. Reinstall VS.
I had the same problem.
I solved it just by reloading the window.
You can either press Ctrl + R or type "Developer: Reload Window" in the command palette.
You can open the command palette either by pressing Ctrl + Shift + P or going to "View"(up left corner) then "Command Palette..."

Need to figure our what a specific symbol is replaced to after macro expansion [duplicate]

Let's say I have a source file with many preprocessor directives. Is it possible to see how it looks after the preprocessor is done with it?
cl.exe, the command line interface to Microsoft Visual C++, has three different options for outputting the preprocessed file (hence the inconsistency in the previous responses about Visual C++):
/E: preprocess to stdout (similar to GCC's -E option)
/P: preprocess to file
/EP: preprocess to stdout without #line directives
If you want to preprocess to a file without #line directives, combine the /P and /EP options.
Most compilers have an option to just run the preprocessor. e.g., gcc provides -E:
-E Stop after the preprocessing stage; do not run the compiler proper.
The output is in the form of preprocessed source code, which is sent
to the standard output.
So you can just run:
gcc -E foo.c
If you can't find such an option, you can also just find the C preprocessor on your machine. It's usually called cpp and is probably already in your path. Invoke it like this:
cpp foo.c
If there are headers you need to include from other directories , you can pass -I/path/to/include/dir to either of these, just as you would with a regular compile.
For Windows, I'll leave it to other posters to provide answers as I'm no expert there.
Right-click on the file on the Solution Explorer, goto Properties. Under Configuration Properties->C/C++->Preprocessor, "Generate Preprocessed File" is what you are looking for. Then right-click on the file in the Solution Explorer and select "Compile". The preprocessed file is created in the output directory (e.g. Release, Debug) with an extension .i (thanks to Steed for his comment).
You typically need to do some postprocessing on the output of the preprocessor, otherwise all the macros just expand to one liners, which is hard to read and debug. For C code, something like the following would suffice:
gcc -E code.c | sed '/^\#/d' | indent -st -i2 > code-x.c
For C++ code, it's actually a lot harder. For GCC/g++, I found this Perl script useful.
I don't know anything about Microsoft compiler, but on GCC you can use this:
gcc -E -P -o result.c my_file.h
If you want to see comments use this:
gcc -E -C -P -o result.c my_file.h
More options avaliable on this page.
Try cl /EP if you are using Microsoft's C++ compiler.
As bk1e and Andreas M. answered, the /P option for the compiler will cause it to preprocess a file. However, in my project using VS2005 and Platform Builder (for an embedded ARM processor), the project did not present an option in the dialog box (as described by Jim B) to enable that option.
I could run CL manually and add /P, but it failed because I did not know all of the appropriate command-line options that were invisibly being activated by Platform Builder during the full build. So I needed to know all of those options.
My solution was to go look in the build.log file, and find the line that executed
CL blah-blah-blah myfile.c
I copied this line to the clipboard. The "blah-blah-blah" part contained the build options, and was huge.
Back in the IDE, I right-clicked on myfile.c, chose "Open Build Window", and then in that window I pasted the build command-line, and added a "/P".
CL /P blah-blah-blah myfile.c
Done. The myfile.i file was produced, which contained the preprocessor output.
In Visual Studio you can compile a file (or project) with /P.
CPIP is a new C/C++ preprocessor written in Python. If you want a detailed visual representation of a preprocessed file, give it a shot.
CPIP is a C/C++ pre-processor implemented in Python. Most pre-processors regard pre-processing as a dirty job that just has to be done as soon as possible. This can make it very hard to track down subtle defects at the pre-processing stage as pre-processors throw away a lot of useful information in favor of getting the result as cheaply as possible.
Few developers really understand pre-processing, to many it is an obscure bit of black magic. CPIP aims to improve that and by recording every detail of preprocessing so CPIP can can produce some wonderfully visual information about file dependencies, macro usage and so on.
CPIP is not designed to be a replacement for cpp (or any other established pre-processor), instead CPIP regards clarity and understanding as more important than speed of processing.
On Windows OS, a simple one line answer to this question is to use the below command in DOS prompt to see the preprocessed file:
CL /P /C myprogram.c
This will generate a file called myprogram.i. Open it and look out for your expanded preprocessors.

Vim: Compiling with gcc

I've been trying to find a solution for my problem for a while now.
I am trying to use gcc to compile my c programs on Windows 7.
My current code
set makeprg=gcc\ %\
gives me "gcc: error: missing filename after '-o'.
How can I get rid of this error?
If your gnumake program is correctly configured (linux, cygwin, ...), but unfortunately not with mingw, you don't need to tweak :makeprg for mono-files pet project compiled where the source files are.
In those cases, if you need to add options, just set them into $CXXFLAGS, $LDFLAGS, $CFLAGS, $CC, $CXX, etc. from vim:
:let $CXXFLAGS='-g -O2 -std=c++11'
Now, back to your problem (as I suspect the badly configured mingw), I'm not sure why you have a backslash at the end of your expression. I'd have used (a plugin, but this is a different story):
" in a c ftplugin/ or a local vimrc in the directory where C pet projects are
:setlocal makeprg=gcc\ %\ -o\ %<.exe

How to generate assembly listings in Code::Blocks?

Can we generate an assembly file in code-blocks for C programs, the same way we can generate listing file when we write C-code for micro-controller?
I presume you want your Code::Blocks build to generate an annotated
assembly listing for each .c source file compiled.
Yes, you can do this as follows (as of Code::Blocks 12.11):
In the Code::Blocks IDE, navigate Settings -> Compiler
Ensure that the Selected compiler is GCC.
In the tab list that begins Compiler settings, Linker settings...,
navigate to the last tab, Other settings.
In the Other settings pane, select Advanced options, at the bottom right.
In the Advanced compiler options windows, ensure that the drop-down menu
entitled Command is set at Compile single file to object file
See the edit box entitled Command line macro and confirm that it
contains the line:
$compiler $options $includes -c $file -o $object
To this line append a space followed by exactly this:
-Wa,-alhds=$objects_output_dir$file_name.list
(Do not allow yourself to be misled at this point by the listing of
Command macros at the left of the window. If it lists $objectsoutputdir
instead of $objects_output_dir and $filename instead of $file_name, it
is wrong.)
Click OK in the Advanced compiler options window and then in the
Compiler settings window.
Rebuild your project.
The effect of the change you have made to the global compiler settings
is that the options:
-alhds=$objects_output_dir$file_name.list
will be passed to the assembly phase when compiling each source file filename.c,
with $objects_output_dirreplaced by the object output files directory of your project
and $file_name replaced by filename, causing the assembler to generate an
annotated listing file filename.list in the same directory where filename.o is placed.
Unfortunately I do not see how to do this just for a selected project; so after
the change the compiler will generate the .list files for all projects.
If this is a nuisance, you can work around it by first configuring a "new compiler"
in Code::Blocks that is a copy of GCC, with whatever name you like, and then applying
the change I have described to this copy. To do this:
Navigate Settings -> Compiler;
Ensure the Selected compiler is GCC
Select Copy and enter the name of your choice when prompted, e.g.
"gcc-list"

Installing Notepad++ C compiler plugin: linintl-8.dll is missing

Following the instructions on http://iitdu.forumsmotion.com/t108-gcc-compiler-in-notepad
However, upon execution of NPPExec with the code provided (below), ld.exe displays a warning detailing that linintl-8.dll is missing.
// The script code, C source code compiling with GNU CC (GCC) in notepad++ (by "NppExec" plug-in) and compressing with UPX...
//
// Enable? "//" signs remove in code line.
// Sample: C:\Program Files\CodeBlocks\MinGW\bin\gcc.exe "$(FULL_CURRENT_PATH)" -o $(NAME_PART)
YOUR_GCC\BIN\_PATH_HERE\gcc.exe "$(FULL_CURRENT_PATH)" -o $(NAME_PART)
// UPX Compress (with "--best, --ultra-brute" options)
// Enable? "//" signs remove in code line.
// Sample: C:\WINDOWS\system32\upx.exe --best --ultra-brute $(NAME_PART).exe
YOUR_UPX_PATH_HERE\upx.exe --best --ultra-brute $(NAME_PART).exe
Yet, I can see linintl-8.dll sitting in the bin directory of mingw.
Does anybody have any idea how this issue can be resolved, or alternatively an entirely different method to successfully compile C in notepad++ ?
I'm no Windows expert, but I know that dlls should go somewhere in C:/System or C:/System32 for Windows 7 you should check this question:
How do I register a DLL file on Windows 7 64-bit?

Resources