ManojMarathayil
ManojMarathayil

Reputation: 732

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)

Upvotes: 7

Views: 11235

Answers (8)

ChrisZZ
ChrisZZ

Reputation: 2141

I'm on Windows, and the /DEFAULTLIB string contains C Runtime Library. Maybe they can be considered as build type.

def strings(fname):
    """
    Remake `strings` command in Python

    This function behaves like `strings` command in linux/windows.
    If no desired result returned, you may just tweak the regular expression pattern.
    ref: https://gist.github.com/berdario/114b2daf9b43fe924676

    Example:
    import arczip
    for word_bytes in arczip.strings(lib_pth):
        word = word_bytes.decode()
        if ('version' in word):
            print(word)
    """
    from mmap import mmap, ACCESS_READ
    import re

    pattern = '([\w/.\s(:)-]{10,200})'
    with open(fname, 'rb') as f, mmap(f.fileno(), 0, access=ACCESS_READ) as m:
        for match in re.finditer(pattern.encode(), m):
            yield match.group(0)


def get_windows_library_build_type(libpath):
    crt_type = None
    for word_bytes in strings(libpath):
        word = word_bytes.decode()
        if 'DEFAULTLIB' in word:
            if 'MSVCRTD' in word:
                crt_type = 'Debug'
                break
            elif 'MSVCRT' in word:
                crt_type = 'Release'
                break
    return crt_type

Upvotes: 0

Tomas Kubes
Tomas Kubes

Reputation: 25098

I use and successfully tested this code:

using System.Diagnostics;

FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(fileName);
return fvi.IsDebug;

Upvotes: 0

joelr
joelr

Reputation: 366

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.

Upvotes: 2

Head Geek
Head Geek

Reputation: 39838

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).

Upvotes: 5

Gilad Naor
Gilad Naor

Reputation: 21516

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.

Upvotes: 0

anon
anon

Reputation:

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.

Upvotes: 1

Abhay
Abhay

Reputation: 7180

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.

Upvotes: 0

Gilad Naor
Gilad Naor

Reputation: 21516

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

Upvotes: 2

Related Questions