gprof output is empty - c

Using gprof 2.28 and gcc 6.3.0 in Ubuntu 17.04 on a variety of sample programs I get empty output for every category. If I run gprof -i on one example program I get:
1 histogram record
2 call-graph records
0 basic-block count records
My compilation looks something like this:
cc -g -c sem_test.c -pg
cc -o sem_test sem_test.o -lpthread -pg
Or this:
gcc -g3 -O0 -Wall -std=gnu99 -pg -fprofile-arcs -fno-inline -fno-reorder-functions sem_test.c -o sem_test -lpthread -pg
Both have the same results.
I notice that my gmon.out file is only 687 bytes which seems low.

This is a glibc bug/limitation:
-pg -pie doesn't work
If you cannot install a fixed glibc, you can link with -no-pie to disable PIE. Your toolchain probably enables PIE automatically.

Related

Compiling C file in OS X

I am not familiar with OS X at all, and I need to compile a C file. Here is the code I use in Linux. What is the OS X version of those?
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG -I/usr/local/include -fpic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -c myfile.c -o myfile.o
gcc -m64 -std=gnu99 -shared -L/usr/lib64/R/lib -Wl,-z,relro -o myfile.so myfile.o -L/usr/lib64/R/lib -lR
Thanks!
You need to install Xcode, which is free, and will allow you to install gcc just by typing gcc in Terminal. From there on, you can just compile .c files using it. Also, you might want to just type gcc myfile.c -o myfile instead of adding all of those flags, because the OS X filesystem hiearchy is different from that of Linux, and adding those extra flags might make the command not work.

DCRaw says I should compile with -O4 in gcc. Does -O4 exist?

I was looking at building DCRaw from the source. On its web page it recommends that I build with one of the following lines.
Compile with "gcc -o dcraw -O4 dcraw.c -lm -ljasper -ljpeg -llcms2" or "gcc -o dcraw -O4 dcraw.c -lm -DNODEPS".
I have never heard of -O4 in gcc. How is that different from -O3?
https://www.cybercom.net/~dcoffin/dcraw/
The maximum level of optimizations with gcc is -O3. Using -O4 (or -O5, -O6, ..., -O9) actually reverts to -O3. There is no guarantee theses options are supported or will behave differently in the future, so just use -O3 for portability.
gcc -c -Q -O3 --help=optimizers > /tmp/O3-opts
gcc -c -Q -O4 --help=optimizers > /tmp/O4-opts
diff -u /tmp/O3-opts /tmp/O4-opts
Currently -O3 is the highest numbered option that actually adds flags. gcc's numbered optimization levels are cumulative. That is, -O3 includes all the -O2 flags and -O2 includes all of the -O1 flags. This leaves the door open for a future -O4 that would include all of -O3 plus more.
The actual flags are in the documentation but I think you get a better insight from the code itself.

How to compile with a .o file that was compiled with other .o files (C99)

consider c.c a code that includes a.h and b.h, and main.c a code that includes c.h
i tried to compile it like so
gcc --std=c99 -o a.o -c a.c
gcc --std=c99 -o b.o -c b.c
gcc --std=c99 -o c.o -c c.c a.o b.o
but when I run the last one, gcc yells at me
gcc --std=c99 -o c.o -c c.c a.o b.o
gcc: warning: a.o: linker input file unused because linking not done
gcc: warning: b.o: linker input file unused because linking not done
and then when I try to compile the main.c file using gcc -o main main.c c.o it says that there are a lot of undefined references, which is predictable once the c file was not correctly compiled.
I've seen some similar questions here at stackoverflow, but I couldn't get it to work neither way.
I'm on Arch Linux running gcc v4.9.2-3
First, it is -std=c99 with a single dash.
I guess you are on Linux.
Then, you always should pass -Wall -Wextra -g (especially since you are a newbie) to gcc : -Wall ask for nearly all warnings, -Wextra for even more warnings, -g ask for debug information.
At last, you want to produce an executable myprog (don't name executables as c.o, this is supposed to be an object file) with
gcc -std=c99 -Wall -Wextra -g -o myprog c.c a.o b.o
You need to remove any -c since you want the linking to happen.
If you really mean -but that is very unusual today, better make shared libraries!- to agglomerate several object files into one all.o (to be linked later with other objects) you might try the -r linker option
gcc -std=c99 -Wall -Wextra -g -r c.c a.o b.o -o all.o
But last time I tried it was in the previous century, so details could be wrong.
There are very few reasons to agglomerate objects using the -r linker option. Unless you really know what you are doing, you are very probably wrong (in trying -r).
Perhaps you want to make a software library. These days it is much better to make a shared library. A shared library (technically an ELF shared object) should contain position independent code. So, assuming you have three translation units t1.c, t2.c, t3.c you first compile them as PIC :
gcc -std=c99 -Wall -Wextra -g -fPIC t1.c -c -o t1.pic.o
gcc -std=c99 -Wall -Wextra -g -fPIC t2.c -c -o t2.pic.o
gcc -std=c99 -Wall -Wextra -g -fPIC t3.c -c -o t3.pic.o
then you link all these PIC object files into a shared library libmyt.so
gcc -std=c99 -Wall -Wextra -g -shared \
t1.pic.o t2.pic.o t3.pic.o \
-o libmyt.so
Later you'll use this shared library e.g. as
gcc -std=c99 -Wall -Wextra -g main.o -o myprog -Wl,-rpath . libmyt.so
or as
gcc -std=c99 -Wall -Wextra -g main.o -o myprog -Wl,-rpath . -L. -lmyt
You might consider static linking with ar to make a static library libmyt.a but I don't recommend that.
Of course, you'll debug your program using gdb ./myprog and you could try running it with ./myprog. To use valgrind, try valgrind ./myprog
If you have several translation units, better learn how to use GNU make. Read the Program Library HowTo and this and these hints.

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

Resources