Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ find out needed parts of code

Tags:

c++

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.

like image 522
tmighty Avatar asked Apr 17 '13 15:04

tmighty


People also ask

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.

What is required in which C program?

The correct answer to the question “What is required in each C program” is, option (a). The program must have at least one function. Any C program will have a function, and function is nothing but a piece of code.


2 Answers

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

like image 151
lucasg Avatar answered Sep 27 '22 21:09

lucasg


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

like image 42
TooTone Avatar answered Sep 27 '22 21:09

TooTone