How to find the Relative Path to other Common Folders – More Easily?

16 12 2008


Well you know what a relative path is. The “.\..\..\Folder” thing. Usually in project settings, while referring the path of other common include folders, relative path is being used. Because, it won’t break the project settings even though the entire development folder structure is moved from one location to another.

To be more specific, If you have specified absolute path like – “c:\Source\BlahBlahProject\Libs” in the project settings, if you move Source folder to D:\ your project won’t compile. Relative paths are good, but its a bit difficult to calculate it. I saw people calculating it with their memory by touching the folder structure in monitor. 😉 Some advanced guys uses the windows explorer’s auto complete feature to get the relative path. But is there any other easy method to get it?

relativepath


Well, You can use Visual Studio for it. Just follow the steps. Assume you want to get the relative path from your project folder to the common lib folder – “c:\Source\BlahBlahProject\Libs”.

1) Load you project in visual studio.
2) Create a file hello.cpp in folder – “c:\Source\BlahBlahProject\Libs”.
3) Add the file to your project by using menu – Project > Add To Project > Files.
4) Right click the file and take Properties.
relativepath1

5) In the properties, you can get the relative path from “Persist as” editbox.
relativepath2


The only limitation is our imagination. Isn’t it? 😉


Targeted Audience – Intermediate.

Advertisements




How to Set one Dll as Delay Loading Dll?

2 12 2008


Nobody like delays. All of us want everything to be on time. May the the most hated characteristic might be the delaying nature. Well, while everyone hates delays, there is one special guy who loves delays – the Windows loader. 😀

delayloadingdll

Well, as you know the purpose of the windows loader is to load the an application file disk to memory and start the execution. While loading an executable, loader have to make sure that all the dlls required by application are also loaded to memory. But assume you have 50 dlls. Eventhough those dlls have to do nothing in application startup, its loader’s duty to load it. And when the dll count goes on, loader have to work more and there by your application needs more time to start. Well, how to make the startup more faster in this situation?


You could mark your dlls as delay loading dlls. If you have enabled delay loading dll option for the dll, then loader loads the dll only when application make the first function call to that dll. Hence, during application startup, the loader’s “load” can be reduced and it will improve startup time. In other words, you can call it as “Load on demand”.

Well for enabling delay loading for a dll, you have to specify the linker option /DELAYLOAD:dllname to linker settings. For instance, see the screenshot for how to enable delay loading for mydll.dll.

delayloadingdll2


Interesting to know that loaders love delays. Isn’t it? lazy loaders 😉


Targeted Audience – Intermediate.





How to Remove Unreferenced Formal Parameter warning?

26 11 2008

Hands on legacy code base is a different kind of game! You have to learn a lot of tricks. During old days, legacy codebases have the compiler default settings. But now, for better error catching, most will switch  the warning level to high. Well, did you ever ported your legacy codebase by updating its project settings such as by switching the warning level from Level 3 (default) to Level 4, most probably you’ll get this – “Warning C4100: ‘Param2’ : unreferenced formal parameter”.

In most of the case, it might be some unused stack variable. But in some other cases, it might be the unused function parameters as well. For instance, if base class have some pure virtual function, we have to implement that, even if we don’t have any specific implementation for it. In that case, the parameters will not be used and can cause the same warning. Well how to “kick out” those warnings easily?

unreferrencedvariablewarning


Well, there are two tricks.

1) Use the macro – UNREFERENCED_PARAMETER
The common trick is to use the macro UNREFERENCED_PARAMETER(). When it expands, it just assigns the variable to itself. Just like a=a. Which will remove the warning, since the variable is used to assign to itself.

// The function that derived class was forced to implement,
// since its pure virtual function defined in base.
void CRabbitDialogDlg::Function( bool Param1, bool Param2 )
{
    // Here Param1 and Param2 are not used which will
    // trigger warning. Now they won't.
    UNREFERENCED_PARAMETER( Param1 );
    UNREFERENCED_PARAMETER( Param2 );
}

2) Comment out the variable names.
Even though UNREFERENCED_PARAMETER() serves the need, its not efficient. isn’t it? It assigns the variable to itself and thus avoids the warning. Well there is another easy method. Just comment out the variable name. Check the code snippet below.

// The function that derived class was forced to implement,
// since its pure virtual function defined in base.
void CRabbitDialogDlg::Function( bool /*Param1*/, bool /*Param2*/ )
{
    // Since the variable name doesn't exist, how compiler can
    // complain that variable is not used. ;)
}


In one of my projects, I was forced write an empty implementation for a pure virtual function in derived class. And my client needs zero compilation warning. At that time I really struggled to remove this warning. 😀 Now when i shared it with you, it feels good!


Targeted Audiance – Beginners.





How to make your debug build – a slim beauty.

1 04 2008

Icon Description
Usually debug builds are bulky. It will be nice, if we can reduce the size of debug binaries before delivery.

Icon How Can I Do It?
If you turn off the “Incremental Linking” option, the size of debug build will be reduced dramatically. But beware, it can increase your total build time.

1) Take project settings.
2) Go to Link tab
3) Disable “Link Incrementally”

Now rebuild and check the size. Wow!!! She became really slim. 😉

Icon Note
1) You have to change the “Settings->C/C++->Debug Info” to “Program Database”. Or else you’ll get a warning.
2) Turning off Incremental Linking can increase your build time. So its recommenced only before delivery.

beginnerseries.jpg
Targeted Audience – Beginners.





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
   StdAfx.obj
   Hello.obj
   kernel32.lib(KERNEL32.dll)
   kernel32.lib(KERNEL32.dll)
   kernel32.lib(KERNEL32.dll)
   ...
   LIBCD.lib(_freebuf.obj)
   LIBCD.lib(close.obj)
   LIBCD.lib(commit.obj)
   LIBCD.lib(fclose.obj)
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”? 😉

advancedseries.jpg
Targeted Audience – Advanced.





See the assembly code generated for your source.

24 03 2008

Icon Description
For die-hard debugging purpouse and for learning the internals, usually we need to refer the assembly code generated by the compiler.

Icon How Can I Do It?
Add compiler switch /FAs in the project settings. It will generate .ASM files for each of your .cpp files with source code and inline assembly. See the following steps.

1) Take project setting by pressing Alt+F7
2) Take C/C++ tab
3) To the Project Options editbox, add /FAs.
4) Recompile the project.
5) Take the release or debug directory, you can see .asm files. 😉