For example:
$ gcc -O3 foobar.c -o foobar
$ grep 'foobar\.c' foobar
Binary file foobar matches
How can I exclude such unnecessary and revealing metadata from the output of gcc and other compilers? It appears regardless of whether the output is an assembly file, object file, or executable.
man strip(1)
> strip -s a.out
Related
I am attempting to compile a .adb file with gnatmake, and the -o flag isn't producing the object file name I want:
$ gnatmake --GCC=g++ -D bin/src/ghdl_grt/ -f -u -c src/ghdl_grt/grt-vstrings_io.adb -o bin/src/ghdl_grt/grt-vstrings_io.adb.o
g++ -c -Isrc/ghdl_grt/ -I- -o /home/jon/controlix-code/bin/src/ghdl_grt/grt-vstrings_io.o src/ghdl_grt/grt-vstrings_io.adb
As you can see, it gets the path correct, but the filename should end with .adb.o and it only ends with .o. Any ideas?
For gnatmake, -o 'chooses an alternate executable name'. But even using gcc (or g++) on its own fails, at any rate on macOS, because gnat1: incorrect object file name.
I found that you can compile to assembler and then compile that. Using a local file I happened to have lying about,
$ g++ -D $PWD -c gator2.adb -S -o gator2.adb.s
$ g++ -D $PWD -c gator2.adb.s
Well, that's a weird naming scheme, but...
gnatmake only allows you to specify alternate executable names with -o:
-o name Choose an alternate executable name
You can, however, tell gnatmake to pass on options to the compiler:
-cargs opts opts are passed to the compiler
And similarly, to the binder and linker:
-bargs opts opts are passed to the binder
-largs opts opts are passed to the linker
Thus,
$ gnatmake --GCC=g++ -D bin/src/ghdl_grt/ -f -u -c src/ghdl_grt/grt-vstrings_io.adb -cargs -o bin/src/ghdl_grt/grt-vstrings_io.adb.o
Is it possible to produce both the object file and the source file in once command with gcc/g++/clang/clang++ ? How?
I need to pass a lot of other options, so I would like to avoid duplicating them in 2 separate commands:
gcc -S test.cc # produce assembly
gcc -c test.cc # produce object file
You can give the -save-temps option to gcc: it will leave all the temporary files (including the .s files) in the current directory (works also with clang):
gcc -c --save-temps test.cc
or use the -Wa,-aln=test.s option:
gcc -c -Wa,-aln=test.s test.c
From gcc documentation:
-Wa,option
Pass option as an option to the assembler. If option contains commas, it is split into multiple options at the commas.
From as documentation:
-a[cdghlmns] Turn on listings, in any of a variety of ways:
-al include assembly
-an omit forms processing
[...]
You may combine these options; for example, use -aln for assembly listing without forms processing.
Clang has an integrated assembler that should be switched off (How to switch off LLVM's integrated assembler?):
clang++ -c -no-integrated-as -Wa,-aln=test.s test.c
The command line limit for windows is ~7000 characters. The clang command for my project is ~130,000 due to a very large number of includes. Is there some way to tell clang about my project includes from a separate file?
I admit to being curious why you have all of the includes on the command line, however, you can use this syntax to put all of the commands in a file:
clang #cmds
where cmds contains something like:
echristo#dzur ~/tmp> cat cmds
-c
foo.c
-o
foo.o
so the full process would look a bit like:
echristo#dzur ~/tmp> ls
cmds foo.c
echristo#dzur ~/tmp> clang #cmds
echristo#dzur ~/tmp> ls
cmds foo.c foo.o
Is this syntax incorrect?
C:\Users\Brett\Compilers>gcc -I MinGW\include -l MinGW\lib\libgdi32.a -o hello
world helloworld.c
The directory's are all fine, I mist be including and linking in the wrong order or something?
Here is the output:
c:/users/Brett/compilers/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw
2/bin/ld.exe: cannot find -lMinGW\lib\libgdi32.a
collect2: ld returned 1 exit status
The syntax for -l switch is the library name without lib prefix and without the extension. If it cannot be found, it's directory should be given with -L option. So I would write:
gcc -I MinGW\include -L MinGW\lib -lgdi32 -o helloworld helloworld.c
Maybe -L is not needed, maybe you also need -mwindows to tell the linker you want windows app. To specify a library file explicitly, give it without any letter option, like this:
gcc -I MinGW\include MinGW\lib\libgdi32.a -o helloworld helloworld.c
Here is the gcc reference: linking options.
I've embedded a text file in a C program using the following method: http://www.linuxjournal.com/content/embedding-file-executable-aka-hello-world-version-5967
a.out:prog.c file.text
objcopy --input binary --output elf64-x86-64 --binary-architecture i386 file.text file.o
gcc prog.c file.o
objcopy requires to specify the target with the "--output" option.
How can I set "--output" in Makefile so objcopy will use the user's architecture ?
Thanks.
Firstly: You are not trying to emulate the -b capability of the GCC ld, are you? In more verbose terms: The GCC ld can actually load a number of binary formats, see the documentation. If that's what you want to achieve, something like:
gcc prog.c -Wl,-b -Wl,binary file.o
might save you the whole objcopy call.
While I'm not able to find documentation on the issue, the output of objdump -i seems to be sorted by preference, so
`objdump -i | head -n 2 | tail -n 1`
should expand to the usual target architecture. Stating again: I have no documentation on this behaviour, so better don't rely blindly on it.