how to use gcc like assembler? - gcc

is it possible? I want to use gcc like assembler and after compile it to executable on ubuntu.
I tried it:
gcc a.asm -o out.o
and from out.o file compiler it to .out executable file.
but I get the following error:
file format not recognized; treating as linker script.
I'm new on linux environment. I hope this is clean for you. Any help is very appreciadted. Thanks in advance.

Change file name a.asm to a.s and let gcc autodetect assembler (by extension).

Read the documentation for the -x option to gcc. It allows you to specify the language of the source file.

Related

GCC ver 7.3.0 preprocessor fails if output file doesn't exist

I tried to build the FreeIPMI on a box with GCC 7.3.0 and got stuck - the preprocessor complained that output file was missing:
nekto#ubuntu:~/freeipmi-1.6.1/man$ /usr/bin/cpp -nostdinc -w -C -P -I../man libipmiconsole.3.pre libipmiconsole.3
cpp: error: libipmiconsole.3: No such file or directory
That's output file, its existence shouldn't be checked I think.
BTW, the same command worked flawlessly on another box with GCC 6.
Is it issue with the GCC 7.3.0 preprocessor?
I'm answering my own question.
The invocation format for the GCC 7.3.0 preprocessor has been changed - the output filename has to be prepended by the -o option, and all the free-standing filenames on the command line are considered input files.
Also the preprocessor became more strict about input formats it supports, so it can't be used to generate man-pages for the FreeIPMI anymore.

How can I generate an ELF file with GCC?

I am writing C and C++ code on Linux OS and I am using GCC. After finishing my code, I would like to generate an ELF file. I just can generate "a.out" file and I don't need it. How can I get ELF file ? ELF file occurs as a result of what ? or Is it possible to generate this file with this program ?
The compiler (i.e. gcc or g++) will invoke the linker (ld) which produces an ELF executable.
In practice, you will use a builder program (like make) to drive gcc commands. See this answer.
The default output file for gcc is still named a.out (for historical reasons) but is an ELF file. And you really want to ask gcc to output an executable with a more fancy name.
Simple example, you code a single-file hello-world.c program. You can compile it with e.g.
gcc -Wall -g hello-world.c -o hello-world-bin
(order of arguments to gcc matters a lot!)
and the produced hello-world-bin is an ELF executable. Check with
file hello-world-bin
then run it with
./hello-world-bin your arguments to it
Later, learn how to use the gdb debugger on it.
See also this and that answers.

Make executable not named a.out?

For example if I run gfortran filename.f90 it will create a.out, but I'd like filename.out instead.
If you need a filename other than the default a.out then you must pass the desired filename after the -o argument to the GCC tool.
gfortran filename.f90 -o filename.out
Try the -o option to allow you to specify an output file name.
More info on the man page, eg:
http://linux.die.net/man/1/gfortran
gfortran shares a lot of command line options with gcc - see here:
http://gcc.gnu.org/onlinedocs/gcc-4.0.1/gcc/Overall-Options.html#Overall-Options

Set gcc and g++ optimization flags permanently

I'm running a x86 kernel on an x64 machine. I would like to compile libraries for a i586 processor. During compilation, some libraries use i686 optimization, so want to set -mtunes=i586, -march=i586 and -O3 flags for all of libraries even if they explicitly declare something else in their makefiles.
Somehow I want to set compiler flags permanently...
Regardless of whether you should do this, here's the easiest way to do it:
Create a new file with the following contents:
#!/bin/sh
exec /usr/bin/gcc "$#" -O3 -mtunes=i586 -march=i586
Change /usr/bin/gcc to your actual compiler if that's not right on your system.
Save it as ~/bin/gcc.
Make the new script executable:
chmod +x ~/bin/gcc
Repeat to create another file for g++.
Add ~/bin to the start of your path:
export PATH=~/bin:$PATH
Compile your project. Whenever your new scripts are on the path they will override whatever the makefile says.
Hope that helps.
P.S. The best way to do it (rather than the easiest) would probably be to mess with the compiler's "specs" file, but it's much harder to explain and do.

Force gcc to use one linker over another

Lately I have been working on OS X. Things were going pretty peachy for a while until somehow ld got on my system and now gcc won't use dyld. Furthermore, all of my shared libraries are in *.dylib format, and ld is stubornly ignoring there existance. If I mv ld from PATH, gcc just complains it cant find ld.
Please help me to get gcc back on track and using what it should.
You can try some gcc options. From the man page:
-c Compile or assemble the source files, but do not link. The linking
stage simply is not done. The ultimate output is in the form of an
object file for each source file.
You could then link explicitly using whatever linker you want.
Does it help to symlink ld to dyld?
mv /usr/bin/ld /usr/bin/ld.old
ln -s /usr/bin/dyld /usr/bin/ld
Edit: fixed ld params order
This isn't your exact question, but I had a need to switch to ld.gold, and for that, the -fuse-ld=gold option to gcc was very useful.
look at -Xlinker option
I got it from man gcc
you can double check using some verbose options like -v

Resources