Automatically link required libraries.

11 04 2008

In our projects we use number of 3ed party API’s especially from platform SDK and other components and one burden is finding the libraries for those API’s. For Microsoft provided API’s since they have strong documentation, finding the library for particular API is not so tedious. But for other vendors, It may be. We need to dig a lot for finding the lib of that particular API. How can we get rid from this?

You can you #pragma comment( lib, “LibraryName” ) to specify the necessary library file in the header file itself. For e.g. See the following header file.

// EventLog.h
class EventLog
#ifdef _DEBUG
    // If it’s a debug version…
    #pragma comment( lib, "MyEventLogLibd.lib" )
    // Ooooh!!! Its release version.
    #pragma comment( lib, "MyEventLogLib.lib" )

If we include the EventLog.h header file, the library required will be automatically included. I dream for the day on which all SDK headers auto includes their libraries… 😉

Target audiance – Intermediate.

How to remove unwanted libraries from project settings.

28 03 2008

Icon Description
Usually in the project settings, we will provide a list of .lib files that the project needs to compile. For small projects it’s manageable. But for big project which uses a lot of 3ed party frameworks, the .lib list can grow and may contain unwanted libraries which are not used any more. Even though an extra library in project setting is harmless, it may end up messing up the project setting. Some clients even have strict project setting rule – project settings should not include unused libraries.

Icon How Can I Do It?
One method is error and trial. Remove each library by hand and compile. If you won’t get any error then you can remove it. Or else you should try Masters Method!

1) Take project settings.
2) Take Link tab.
3) In the “project options” editbox add the switch – /verbose
4) Rebuild the project
5) Now in the output window you can see a lot of messages other than normal build.
6) Find the pass 2 section in the output messages. It will be almost same as follows

Start Pass2
End Pass2

7) In the pass 2 section, linker will emit all used libraries.
8 ) In the above e.g. the lib files needed
are “kernel32.lib” and “LIBCD.lib”
9) So you’ve found the required library files!
10) Add them to “object/library modules:” editbox of linker settings.
11) Don’t forget to enable “ignore default libraries” options of linker. Because by default the linker links a set of default libs to the project. Those libs are no more needed!
12) Your project is free from unwanted libraries.

Now the choice is yours.Want to go for “Error and trial” or “Masters Method”? 😉

Targeted Audience – Advanced.