How do you use PGO + LTO optimization with GCC - c

I've been researching a lot lately on how PGO and LTO can significantly optimize a programs speed (some say around 20%). I currently just program in C and build GUI's with GTK+ in Windows (compiling everything through GCC) and only use -O2 for optimization.
I've been reading to compile with the flags
gcc -Wall -g -mwindows -O3 -fprofile-generate -flto example.c -o example.exe `pkg-config --cflags --libs gtk+-3.0`
a .gcno and a .gcda file is created, then you run the compiled .exe a bunch of times then just re-compile the same .c file again but swap the "-fprofile-generate" with "-fprofile-use"? (while making sure all these files stay in the same folder/directory, and use a different name for the .exe compiled the second time)
gcc -Wall -g -mwindows -O3 -fprofile-use -flto example.c -o program.exe `pkg-config --cflags --libs gtk+-3.0`
Or do you make use of the .gcno/.gcda file somehow? Also read things about using a "benchmark".
Question:
What are the steps to use it in GCC? (Step by step guide would help :)

Related

SDL 2 C Compiler Flags

Whenever I run the following, I get undefined references to all the SDL-related functions used in my program:
cc -lSDL2 -lGL *.o
I believe this is caused by the lack of -l linker flags.
GCC arguments are positional, put the link flags after your o files:
gcc *.o -lSDL2 -lGL
Also, if you're on a proper full Linux system I'd recommend using pkg-config to pull compiler/linker flags:
gcc -c main.c `pkg-config sdl2 --cflags`
gcc main.o `pkg-config sdl2 --libs`

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.

what is the significance of pkg-config in gcc?

While building a dbus-example, I found that we need to add a pkg-config in gcc. For example:
gcc `pkg-config --cflags --libs dbus-1` <file_name> -o <file_name.out>
What is the significance of pkg-config --cflags --libs dbus-1? what is pkg-config here? what is cflags extra here? what is --libshere?
gcc `pkg-config --cflags --libs dbus-1` <file_name> -o <file_name.out>
will run the pkg-config command, and pass its output as parameters to gcc.
The purpose of pkg-config is to make linking against libraries much easier, as different operating systems and distributions require different compilation flags (aka CFLAGS), library inclusion paths and libraries to link to. pkg-config uses configuration files (defined by the libraries) to generate the above information for compilers, and allows us to not worry about what operating system or distribution the compilation takes place on.
--cflags means the pkg-config should give the compilation flags for the listed packages.
--libs means the pkg-config should give the linking information for the listed packages.
and dbus-1 is the name of the package.
gcc `pkg-config --cflags --libs dbus-1` <file_name> -o <file_name.out>
comprises these parts:
executing the pkg-config --cflags --libs dbus-1 note `` run the command in between.
run gcc with the flags 1. returns and an input file <file_name> output object file .

Why can't I build a "hello world" for glib?

So here's the world's simplest glib program:
#include <glib.h>
I try to compile it with gcc test.c and I get:
test.c:1:18: error: glib.h: No such file or directory
So I make sure that I have the right packages:
# dpkg -l | grep libglib
ii libglib-perl 1:1.183-1 Perl interface to the GLib and GObject libra
ii libglib1.2-dev 1.2.10-19build1 The GLib library of C routines (development)
ii libglib1.2ldbl 1.2.10-19build1 The GLib library of C routines
ii libglib2.0-0 2.20.1-0ubuntu2 The GLib library of C routines
ii libglib2.0-cil 2.12.1-1ubuntu2 CLI binding for the GLib utility library 2.1
ii libglib2.0-data 2.18.2-0ubuntu2 Common files for GLib library
ii libglib2.0-dev 2.20.1-0ubuntu2 Development files for the GLib library
ii libglibmm-2.4-1c2a 2.18.1-1 C++ wrapper for the GLib toolkit (shared lib
Then I search for any "glib.h" anywhere under /usr/include. I get two, /usr/include/glib-1.2/glib.h and /usr/include/glib-2.0/glib.h. So I try:
$ gcc -I/usr/include/glib-2.0 -Wall test.c
In file included from /usr/include/glib-2.0/glib/galloca.h:34,
from /usr/include/glib-2.0/glib.h:32,
from test.c:2:
/usr/include/glib-2.0/glib/gtypes.h:34:24: error: glibconfig.h: No such file or directory
(about 10,000 more errors snipped)
I don't seem to have a glibconfig.h anywhere on my computer.
What do I do now?
glib tends to hide itself... Your include statement doesn't work because GCC doesn't automatically search subdirectories, and so cannot see the glib.h in glib-1.2 or glib-2.0.
Read the Compiling GLib Applications page in the GLIB manuals... you use commands like pkg-config --cflags glib-2.0 to get the right flags for GCC.
The canonical way to do what you are trying is
% gcc test.c -Wall -o test `pkg-config --cflags --libs glib-2.0`
Note the back-ticks, which tell the shell to run the pkg-config command "in-place".
> > The canonical way to do what you are trying is
> % gcc test.c -Wall -o test `pkg-config --cflags --libs glib-2.0`
Sorry, but no. That is a common misconception, that just happens to work in most cases on ELF-based systems, Linux in particular. The canonical way is to pass in the cflags and libraries separately, in the correct and traditional locations on the command line, like this:
gcc -Wall -o test `pkg-config --cflags glib-2.0` test.c `pkg-config --libs glib-2.0`
It is a pity that pkg-config accepts both the --cflags and --libs options at the same time, as it means this incorrect meme will never die, and people used to it on Linux will continue to be baffled when they then try the same on other platforms.
As #chris said use pkg-config.
glibconfig.h is missing
it’s because this file is not in the /usr/include/glib-2.0, but in /usr/lib/glib-2.0. So you have to include also this /usr/lib path or copy the file to the /include/glib-2.0
I am using glib.h as well-
Do this to compile all your glib.h programs :)
gcc `pkg-config --cflags --libs glib-2.0` filename.c
Make sure to surround pkg-config --cflags --libs glib-2.0 with back-quotes
which you find under tilde (left most key on the querty keyboard).
Thank me later .. :P
apt-get build-dep is your friend -- nobody can remember all the packages you need to build something.

Resources