Tutorial :How to identify if a library is DEBUG or RELEASE build?


Our project is using many static libraries to build the application. How can we make sure we are using release version of libraries in release build of application?

We are making mistakes by taking debug library in release application build.

I am looking for an elegant way in which I can write a module in that we can check whether a particular library is release or debug and report it if not matching. Our application is written in C/C++. (Platform MSVC & GCC)


The normal approach is eithr to give the libraries different names or store them in different directories, such as Debug and Release. And if your build is correctly automated, I can't see how you can make mistakes.


Yes. You can check the Characteristics field of the IMAGE_FILE_HEADER structure of the file. If the library is a release build, then bit 0x0200 (DEBUG_STRIPPED) will be set; on a debug build, it will be clear.

You can find technical information on the PE Format used by Windows EXEs and DLLs, to see how to retrieve that structure, in various places on the 'net (such as here).


Can you not solve this using naming conventions (i.e., foo_rel.a and foo_dbg.a )?


Naming conventions aside, if you're on a unix-like system, you can probably parse the output of:

objdump -g mylib.a  

If you only get empty lines or lines starting with object file names, then you have no debug information in the library.

Note that this does not generally mean that the library is "release" or "debug", but it may mean it in your case.


How about having a simple function which returns the version of the library? Return different things based on your build being debug or release. Call that function at the start of your app and report the error.


Normally one would distinguish the versions using a slightly different names. For example under debug builds all the libraries are suffixed with a character 'd' before their extension. Ex. commonUtilsd.lib Under release mode the same would be commonUtils.lib. This approach IMHO is simpler and cleaner. In MSVC use can specify the output filename under

Librarian-->General-->Output File  

Another recommendation is to have these output files in the configuration directory. i.e. have debug-version in the Debug folder and release-versions under Release folder. Again under MSVC this can be done generically using the $(ConfigurationName) IDE-macro. And attach the right path in the lookup directories during build.


I use and successfully tested this code:

using System.Diagnostics;    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(fileName);  return fvi.IsDebug;  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »