-2

I have mylib.lib:

//mylib.h:
int foo();

//mylib.cpp:
#include "mylib.h"

const int arr[] = {1, 2};

int foo() {
    return arr[0];
}

And proxylib.lib:

//proxylib.h:
int bla1();
int bla2();

//proxylib.cpp:
#include "../mylib/mylib.h"
#include "proxylib.h"

int bla1() {
    return 2;
}

int bla2() {
    return foo();
}

and the exe project:

//main.cpp
#include "../proxylib/proxylib.h"
#include <iostream>

int main() {
    std::cout << bla1();
    return 0;
}

The main project has reference to proxylib.lib project, and proxylib project has reference to mylib.lib project. After compiling and linking the project to the final exe, I got the unused global variable "arr" from mylib.lib in the exe even though the function "foo" isn't in the exe. WHY ?

Thanks !

daPollak
  • 157
  • 3

1 Answers1

0

main.cpp calls bla1() declared in proxlib.h. Proxlib.h includes mylib.h which has function are defined in mylib.cpp, so this gets compiled too.

benrules2
  • 417
  • 4
  • 14
  • foo isn't called in the main function. foo IS linked in the final exe, because you have called bla1 defined in proxlib that includes mylib. Remove bla2 and #include "../mylib/mylib.h" from proxlib to confirm. – benrules2 Mar 09 '16 at 20:58
  • I don't get your point. bla2 is unused function because there is no reference to it, hence it shouldn't be linked into the exe, hence foo isn't linked (I can see that on IDA). but the global "arr" is in the exe, and it is not referenced by any function in the binary (checked on IDA too) – daPollak Mar 09 '16 at 21:18
  • I'm not sure you're understanding how compilation takes place quite correctly. It doesn't matter you don't actually USE the objects, the fact is they are included in the compilation because their source files are included. Checkout this great explanation: http://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work If you're using gcc, you can ignore this warning using the -Wno-unused-variable switch – benrules2 Mar 09 '16 at 21:23