Scenario 1:
I want to link an new library (libA) into my program, libA was built using gcc with -std=gnu99 flag, while the current libraries of my program were built without that option (and let’s assume gcc uses -std=gnu89 by default).
Scenario 2:
libB was built with some preprocessor flags like “-D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED” to enable XPG4 features, e.g. msg_control member of struct msghdr. While libC wasn’t built without those preprocessor flags, then it’s linked against libB.
Is it wrong to link libraries built with different preprocessor flags or C standards ?
My concern is mainly about structure definitions mismatch.
Thanks.
Scenario 1 is completely safe for you.
std=option in GCC checks code for compatibility with standard, but has nothing to do with ABI, so you may feel free to combine precompiled code with different std options.Scenario 2 may be unsafe. I will put here just one simple example, real cases may be much more tricky.
Consider, that you have some function, like:
And you compile
a.owith-DMYDEFandb.owithout, and functionbarfroma.ocalls functionfooinb.o. Next you link it together and everything seems to be fine. Then everything fails in runtime and you may have very hard time debugging why are you passing int from one module, while expecting float on callee side.Some more tricky cases may include conditionally defined structure fields, calling conventions, global variable sizes.
P.S. Assuming all your sources are written in the same language, varying only std options and macro definitions. Combining C and C++ code is really tricky sometimes, agree with Mikhail.