[svlug] [GCC Question] Compiling different objects with different optimization level

Dan Martinez dfm at razorwind.org
Fri Mar 11 18:23:05 PST 2005

> I've this question during developing a linux project with GCC.
> Basically is that OK for us to compile different objects (.c files)
> with different optimization level and link them together?

Generally speaking, yes.

> Does the library my main program linked to need to be compiled at the
> same optimization level as my main program?

Generally speaking, no.

> Does it make any difference if it's kernel code instead of user-space
> code?

Not that I'm aware of, but I'm no kernel hacker.

Basically, it boils down to this: the optimization-level flags tell the 
compiler how much leeway it has in generating machine code to favor a 
particular kind of efficiency. (Speed is one kind of efficiency. So is 
space. Which you care about more depends on your target audience and 

In other words, there's nothing that gets placed in the resulting object 
file that says, "I was compiled at optimization level thus-and-so, and 
will not play with anything compiled with different values."

I say "generally speaking" above because it's possible to break just 
about anything with sufficiently wacked-out -- which is to say, overly 
aggressive -- optimization flags. But for sane conditions of use, you 
should be fine.

You might, if you have it handy, find the section of the gcc info file 
on "Compatibility" edifying. (Try running "info gcc" on your system, and 
look for an item titled "Compatibility".


More information about the svlug mailing list