Reputation: 1
I am working with a couple of projects that rely on class libraries. A class (of course) might use other classes that might be present or not. This is handled by the linker in a sensible way.
(edit)
Suppose a project with a single source file:
#include <iostream>
using namespace std;
class foo
{
public:
void function1(); // implementation missing
void function2(); // implementation missing
};
class bar // not used
{
void bar_fcn()
{
auto p = new foo();
p->function1();
p->function2();
}
};
int main()
{
cout << "!!!Hello World!!!" << endl;
return 0;
}
Class bar uses the missing functions. The linker ignores this silently because class bar is not referenced.
However, with stand-alone functions, this does not work.
Modifying the source file to
#include <iostream>
using namespace std;
class foo
{
public:
void function1(); // implementation missing
void function2(); // implementation missing
};
void bar() // not used
{
auto p = new foo();
p->function1();
p->function2();
}
int main()
{
cout << "!!!Hello World!!!" << endl;
return 0;
}
the linker generates two "undefined reference" errors for the two missing functions.
I tried the -unused_section_elimination and -ffunction-section where unused functions should be eliminated, but the errors remain.
What is the theory behind that? How should the linker handle unused functions vs. unused classes? Is there any documentation on this?
I tried this with both an LLVM compiler and with MinGW.org GCC-6.3.0-1.
Upvotes: 0
Views: 26