tmighty
tmighty

Reputation: 11399

C++ find out needed parts of code

I have a really huge solution in VS2010 C++ with several projects, and I only want to keep those parts that are actually needed.

It is difficult because some parts of one project need other parts of other projects.

Does anybody know how I could automatically remove those parts of the code that are not called when I run the application, perhaps using an add-on for VS2010 or so?

I guess this is a rather unusual wish, but perhaps somebody knows a solution anyway.

Thank you.

Upvotes: 12

Views: 387

Answers (3)

lucasg
lucasg

Reputation: 11012

Not an automatic solution, but I used doxygen Graph feature to build dependency tree for project class : doxygen doc on the subject

Upvotes: 4

TooTone
TooTone

Reputation: 8126

It's worth playing around with the link options to see if they can help you: the linker discards code in statically linked libraries that it doesn't use. It has to do exactly the job that you want, i.e. take the closure of everything that's used and discard the rest. I got this working in visual studio 2008 in order to delete some unused code from a library I was maintaining.

I used /OPT:REF /VERBOSE on the linker command line, and then searched the output for "Discarded .* from MYLIB.lib" using a regular expression. I tried it in visual studio 2010 just now (I don't have 2012), and it was a bit different from 2008. I needed /OPT:REF /VERBOSE:REF, and it only seemed to work under debug, because presumably the link-time optimisations are getting in the way (under release it said it was discarding functions that were used!). Anyway it's worth looking into in more detail because it did work under 2008. I would be interested to hear if you get anywhere (you might want to try a simple, test project first).

EDIT: I have visual studio 2012 at home so tried it out.

Created a static library testlib with test.h

void used();
void unused();

and test.cpp

#include "test.h"
#include <stdio.h>

void used_by_used()
{
    printf("used_by_used");
}

void used()
{
    used_by_used();
    printf("used");
}

void used_by_unused()
{

    printf("used_by_unused!!!!");
}

void unused()
{
    used_by_unused();
    printf("unused!!!!");
}

and a console application with cpp file

#include "../Win32Project1/test.h"

int _tmain(int argc, _TCHAR* argv[])
{
    used();
    return 0;
}

Then for a debug build, in the link options for the console application set References to Yes (/OPT:REF). Then add "VERBOSE:REF" to the command line options. In the linker output, amongst a lot of junk you get

1>      Discarded "void __cdecl unused(void)" (?unused@@YAXXZ) from testlib.lib(test.obj)
1>      Discarded "void __cdecl used_by_unused(void)" (?used_by_unused@@YAXXZ) from testlib.lib(test.obj)

and based on what I did with visual studio 2008 this hopefully ought to do the job. (I see now your question actually specified visual studio 2010. I'm guessing this technique will work in 2008, 2010, and 2012 with modifications as noted.)

Upvotes: 4

tmighty
tmighty

Reputation: 11399

I have - for my situation - a quite good solution. My solution contains around 20 projects, and I can unload each project (so that it appears greyed out). If my code then still works, I know that I can savely remove it.

Upvotes: 1

Related Questions