Would gcc -O take effect for linked static library? - c

such as:
gcc test.c mystaticlib.a -o test -O2
would -O2 take effect for mystaticlib.a, or just test.c?

-O2 is a compilation flag. The only input you're compiling in this example is test.c. mystaticlib.a is not compiled, but rather linked with the compilation output of test.c to create the executable test. Since mystaticlib.a isn't compiled here, the -O2 flag does not affect it.

Related

Can't name executable with specified optimization

I have to turn off optimizations while compiling c code I wrote while using the gcc compiler on a linux. I have found that I can compile the code but I can only get the code to compile without the executable name specified (default to a.out).
So this works:
gcc -O0 Problem04b.c
But my problem is that I have to submit this assignment and I can't submit an executable called a.out because my instructor needs to know which problem it is. I realize I can probably just run
cp a.out Problem04b
then
rm a.out
but I want to know if there is a way I can just compile the code directly into the executable Problem04b. I've tried to run the command like this:
gcc -O0 Problem04b Problem04b.c
but I'm having no luck.
Thanks for your help.
It's the -o flag:
gcc -O0 -o Problem04b Problem04b.c
To specify the output file, you need to use the -o <filename> option with gcc.
Note : Please mind the lower case here
In your case, it should be
gcc -O0 -o Problem04b Problem04b.c
For reference: From gcc manual
-o file
Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.
Actually, you also want to get warnings (that won't change the produced executable, but it is very helpful to you), so compile with
gcc -O0 -Wall -Wextra Problem04b.c -o Problem04b
The -Wall option asks for nearly all warnings, the -Wextra option asks for even more of them.
To run your thing (the ./ is useful because of possible PATH issues):
./Problem04b
Notice that -O0 is optional (since it is the default), you could remove it.
gcc -Wall -Wextra Problem04b.c -o Problem04b
If you want real optimization, e.g. for benchmarking, use e.g. -O1 or -O2 or -O3
You probably want to compile with debug information, then
gcc -g -Wall -Wextra Problem04b.c -o Problem04b
and of course you need to learn how to use the GDB debugger. So read some tutorial about that, then type
gdb ./Problem04b
You'll get a (gdb) prompt. Try help at that time.
You probably want to read the chapter about invoking GCC of the GCC documentation.

Compiling C code in Linux terminal

I am using Linux mint 16. I had a code that I change it a bit.
I use two following commands in terminal in order to run the code. The problem is that it does not give me any error but the changes are not applied, which means it runs the previous version of code.
gcc -std=c99 -c Code.c -o Code.o
./Code
gcc -std=c99 -c Code.c -o Code.o will put the compiled object file in Code.o, not ./Code as you expect it to be..
Also, -c tells do not run the linker. So effectively you end up with an object file which cannot be run.
gcc -std=c99 Code.c -o Code will produce what you need.
For a complete list of gcc flags either use man gcc or see http://linux.die.net/man/1/gcc
Try
gcc -std=c99 -c Code.c -o Code
./Code

Is it possible with -Wall but only display warnings in specific files?

I am writing a project in C using GCC 4.8 and I would like to see all the warnings (hoping to eliminate them) but the problem is I am #including some old, not maintained library which gives me huge wall of warnings in reaction to -Wall option. There is no way I fix those and I just want to ignore it focusing on code I actually write/maintain.
So can I:
gcc -Wall-excluding-OldBlackBox.c -myproject.c ?
Update your makefile so that you have a different gcc -Wxxx line for different files (or groups of files)
result.exe : xxx.o yyy.o
gcc -o result.exe xxx.o yyy.o
xxx.o : xxx.c
gcc -Wall xxx.c
yyy.o : yyy.c
gcc -W yyy.c
first create individual object files and then link them as single Executable.
//compilation with warnings and compilation without warnings
gcc -Wall file1.c file2.c -o foo.o && gcc -w file3.c file4.c -o foo1.o
gcc -o final foo.o foo1.o

static linking with cross compiler

My test.c program uses printf function. I want to statically link library I need. I use arm-linux-gnueabi-gcc cross compiler.
When I compile my code like this
arm-linux-gnueabi-gcc test.c -o test
it passes, but I think he still uses dynamic linking by looking at dissasembly of test.
So,what option should I add in order to statically link libc.a library?
Try
arm-linux-gnueabi-gcc test.c -o test -Xlinker -static /path/to/libc.a
This worked for me. Also, it might complain that you don't have static libgcc_s. Then try this:
arm-linux-gnueabi-gcc test.c -o test -Xlinker -static /path/to/libc.a -static-libgcc
Hope this helps!

gcc makefile won't compile

My final executable (this is in unix though) will be proj07.
proj07: /user/cse320/Projects/project07.driver.o proj07.support.o
gcc -Wall /user/cse320/Projects/project07.driver.o proj07.support.o
proj07.support.o: proj07.support.c
gcc -c proj07.support.c
This creates proj07.support.o but no proj07 exists after compilation. I don't get an error so my mistake must be simple but I can't seem to figure it out.
Here's the output:
gcc -c proj07.support.c
gcc -Wall /user/cse320/Projects/project07.driver.o proj07.support.o
Also I am to use a static driver to test my file which is why the path is like that
You probably do have an a.out executable. Add -o $# to your first gcc occurrence and you should be fine.

Resources