2

My application links statically against several libraries such as libpng, zlib, jpeglib, freetype, etc. I do not use any IDE or build tool such as CMake but just makefiles. I'm using gcc on Linux, Android, and Mac OS and Visual C on Windows. My target architectures are x86, x86_64, powerpc, and arm. My application and the link libraries are pure C, no C++.

After installing a new version of gcc or Visual C, I typically recompile all those link libraries I statically link my application against with the new compiler version although I've always wondered whether this is really necessary.

That's why I'd like to put up this question: Do static link libraries have to be compiled with the same compiler version as my application or can they also be compiled with older compiler versions without causing any harm?

Andreas
  • 9,245
  • 9
  • 49
  • 97
  • 3
    C libraries tend to be ABI compatible. Therefore, you don't have to recompile your C libraries unless the ABI changes (it doesn't). – Bjorn A. Jul 30 '17 at 16:30
  • It is not needed unless for example you want to use something new (like better optimization). – 0___________ Jul 30 '17 at 16:31
  • I've seen cases where a new compiler exposes bugs in old libraries or some incompatibility issues. But those are seldom. So, you can try to link with old libs. But if you see strange failures in your application, you might want to recompile them. – Serge Jul 30 '17 at 17:04
  • @BjornA. the ABI can be changed with compiler flags , e.g. gcc allows `wchar_t` to either be 2 or 4 bytes; and `char` can be set to either signed or unsigned. IMO it's good practice to keep rebuilding things when you upgrade compiler. The worst case scenario is when your program breaks obscurely, you go to rebuild and find some library doesn't even compile any more and wouldn't have compiled for some time. – M.M Jul 30 '17 at 21:36
  • 1
    Don't know about gcc, but with MSVC you generally do need to rebuild all static libraries. Each MSVC version comes with its own distinct version of C++ runtime; and all modules linked into an executable should use the same runtime, or else weird things start to happen. – Igor Tandetnik Jul 31 '17 at 01:36
  • @Igor: Note that I'm not using any C++, just plain old C. – Andreas Jul 31 '17 at 08:35
  • C runtime, same thing. You a likely calling `malloc` and `free`, or passing around `FILE*` handles, or using other CRT resources. – Igor Tandetnik Jul 31 '17 at 12:54
  • Ok, so some people say I have to rebuild them, while others say I don't have to do that. Hmm, I guess I just keep rebuilding them in that case :) – Andreas Aug 01 '17 at 11:53

0 Answers0