Yongxin
Yongxin

Reputation: 173

Compile with different optimzation levels for different parts of a code

I understand that compiling a code with different optimization levels, e.g. -O2 and -O3, can generate different assembly code and consequently cause different performance. For a large project, we often have several third-party libraries, which are often well tested. We normally choose the Release type to build the dependencies, i.e. with -O3 flag. For our newly developed parts, we could use other optimization levels instead of -O3 for debugging and testing reasons. I am wondering if there are other implications for using different optimzation levels on different parts of a code besides the downgrade of performance.

Upvotes: 0

Views: 227

Answers (1)

Peter Cordes
Peter Cordes

Reputation: 363942

In terms of correctness, there shouldn't be. It would be a compiler bug if an optimization level changed the ABI.

It's 100% normal to make test/debug builds at -O0 or -Og and link against optimized libraries (including but not limited to system libraries like libc).

You only need -O3 -flto -march=native etc. etc. when testing / optimizing performance.

Upvotes: 1

Related Questions