Although, one can imagine such an optimization, I haven't heard of such and I really doubt it, because any system call is usually very expensive.
If you are on a *nix system, you can verify it by looking for undefined symbols with nm
nm -u file1.o file2.o | grep socket
should show somewhere the missing socket
symbol as
U socket
if there is somewhere a call to socket.
As I mentioned, I doubt, that there is an optimization inserting any system call and I expect no output from the command line above.
Update:
On my system (Ubuntu 12.04, gcc 4.6), I found the following note in man gcc
-O2 Optimize even more. ...
NOTE: In Ubuntu 8.10 and later versions, -D_FORTIFY_SOURCE=2 is set by default, and is activated when -O is set to 2 or higher. This enables additional compile- time and run-time checks for several libc functions. To disable, specify either -U_FORTIFY_SOURCE or -D_FORTIFY_SOURCE=0.
So, maybe through this or a similar mechanism, there is some code included when the optimization is set to -O2
or -O3
.