cmake debugging -g -Og -Og settting CMAKE_C(XX)_FLAGS_DEBUGS - debugging

my CMAKE_C_FLAGS_DEBUGS and CMAKE_CXX_FLAGS_DEBUG flags in the end result in -g -Og -Og, when removing one -Og I have to build again, while i thought that only the last -Og would be taken. This makes me think that there is a difference in behaviour between the two.
Can someone explain this behaviour. It is caused by the cmake-gui which has its own settings, which i did not expect.

CMake doesn't look at the specific flags you're passing, since that would require understanding command-line options for all the compilers it supports. Because you changed the flags, even in a manner that's logically equivalent from a gcc perspective, CMake considers everything built with the old flags out of date.

Related

where to add -g -O0 debug flags for lldb

I've been told that -g, -O0 tell the compiler not to optimize the code, in order to generate the executable the clearest possible. If I need to create explicitly the object file with -c option, do I have to add debug flags in that step or in linking, or both?
-g tells the compiler to generate debug information, -O0 tells it to not optimize the code; those two options can be used independently from each other. In order to generate straightforward code, it suffices to use the -O0 flag in the compiling (-c) step, since that's where the code is generated. Whether -g is needed in the linking step depends on the toolchain - it is not needed with a GNU linker.

GCC, compare the effect of using -O2 and using all the optimization flags it turns on

From gcc5.4 documentation, it says
-O2 turns on all optimization flags specified by -O. It also turns on the following optimization flags:
-fthread-jumps
-falign-functions -falign-jumps
-falign-loops -falign-labels
-fcaller-saves
-fcrossjumping
-fcse-follow-jumps, etc
It seems that using -O2 has the same effect of using all the 83 optimization flags turned on by -O2 in gcc 5.4.0 on the performance of the test programs.
However, I compare the running time of the executable files test1 and test2 obtained by
gcc-5.4 -O2 test.c -o test1
and
gcc-5.4 -fauto-inc-dec
-fbranch-count-reg
-fcombine-stack-adjustments
-fcompare-elim ... -fthread-jumps -falign-functions ...(all the 83 flags) test.c -o test2
I tested on 20 random generated c programs and running each test case 100000 times to make sure the measurement of running time is accurate enough. But the result is that using -O2 is averagely about 60% faster than using all the 83 flags.
I am really confused why the effect of using -O2 is not equivalent to using all the optimization flags it turns on.
I must misunderstood something, but I couldn't find any explanation yet. I'd appreciate any help. Thanks a lot.
It is a common gotcha. In order to enable (or disable) specific optimizations, you must first enable the optimizer in general, i.e. use one of -O... flags, except -O0 (or just -O, which is equivalent to -O1).
The optimisation level affects decisions in other parts of the compiler besides determining which passes get run. These can be during mandatory processes like transforming between internal representations of the code, register allocation etc, so the optimisation level is not exactly equivalent to a set of switches enabling every compiler pass.
Look at this thread for some discussion on this topic.

How to tell what gcc/autotools options from CFLAGS also go in LDFLAGS?

I have some -f compiler options in CFLAGS like -fsanitize=address and someone suggested to me a while ago that I also put those options in LDFLAGS so I did. It hasn't caused any problems.
My question is how can I tell what compiler options from CFLAGS also should go in LDFLAGS? Is it just the -f prefixed options? Thanks
I have some -f compiler options in CFLAGS like -fsanitize=address and
someone suggested to me a while ago that I also put those options in LDFLAGS
so I did. It hasn't caused any problems.
Not only it won't cause any problems but that's the only correct way to build sanitized program.
My question is how can I tell what compiler options from CFLAGS
also should go in LDFLAGS? Is it just the -f prefixed options?
It won't hurt if you simply do
LDFLAGS = $(CFLAGS) $(LIBS)
Compiler driver (/usr/bin/gcc) knows which high-level options are for the linker so unnecessary options will be simply filtered out. In case you wonder how this happens, run gcc with -dumpspecs flag:
$ gcc -dumpspecs
...
*link_command:
%{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S: %(linker) %{!fno-use-linker-plugin:%{!fno-lto: -plugin %(linker_plugin_file) -plugin-opt=%(lto_wrapper) -plugin-opt=-fresolution=%u.res %{!nostdlib:%{!nodefaultlibs:%:pass-through-libs(%(link_gcc_c_sequence))}} }}%{flto|flto=*:%<fcompare-debug*} %{flto} %{fno-lto} %{flto=*} %l %{no-pie:} %{pie:-pie} ...
This looks a bit ugly but in reality that's just a mini-language for flag translation. Syntax details are given here (although I seriously doubt that you need need to know about them).

Disable optimizations for a specific file with autotools

I'm working on setting up autotools for a large code base that was once just a bash script compile and later just hand written Makefiles.
We have a set of files that require that compiler optimizations be turned off. These files are already in their own subdirectory, so they will have their own Makefile.am.
What's the proper way to drop any existing compiler optimizations and force a -O0 flag on the compiler for these specific files?
I went with Brett Hale's comment to use subpackages. I was able to insert
: ${CFLAGS="-O0"}
before AC_PROG_CC, which sets the appropriate optimization. The other solutions do not work, since the -g -O2 was getting added very last. You can never get another -O variable after it.
You don't have to remove existing optimizations: the last value of -O on the compiler invocation will be used, so it's good enough to just add -O0 at the end.
This is not directly supported by automake, but there's a trick you can use defined in the documentation.
Otherwise if you know you'll only ever invoke your makefile with GNU make you can play other tricks that are GNU make specific; you may have to disable automake warnings about non-portable content.

Using -flto with autotools

Given a C++ program that uses GNU autotools, what's the easiest way to compile it with -flto (link time optimization)? My understanding is that it is customary on Unix for such optimization flags to be specified by the user or packager, not by the programmer.
According to this post, the -flto flag needs to be passed as a compilation flag and as a linker flag, so:
./configure CXXFLAGS="-flto" LDFLAGS="-flto" ...
or possibly:
./configure CXXFLAGS="-flto" LDFLAGS="-Wc,-flto" ...
might work.

Resources