Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to ignore unused undefined references?

Suppose I have two source files — UndefErr.cpp:

#include <cstdio>

void UndefFunc();
void Func2(){UndefFunc();}

void Func1(){printf("Hi\n");}

And the main.cpp:

void Func1();

int main(){
    Func1();
    return 0;
}

As you see in the UndefErr.cpp the Func2() going to trigger an error, for it using the undefined UndefFunc(). However the main function doesn't care about the Func2()! According to a relevant question I could pass an option --unresolved-symbols=ignore-in-object-files to the linker, but I want a kinda different thing. I need a linker to know if the undefined functions used somewhere, and only then fail.

The reason for asking such a strange question is that I'm trying to use a lwIP, and it is hard to understand all of its dependencies (I only need TCP/IP), and I can't find tutorials on the internet. So I thought I could compile most (or all) the .c files separately, and write some simple tests to see what it does. But this approach stumbles upon "undefined references", most of them probably irrelevant to the usecase.

like image 430
Hi-Angel Avatar asked Jun 11 '14 11:06

Hi-Angel


People also ask

How do you resolve an undefined reference?

The error: undefined reference to function show() has appeared on the terminal shell as predicted. To solve this error, simply open the file and make the name of a function the same in its function definition and function call. So, we used to show(), i.e., small case names to go further.

How do you fix undefined references in C++?

You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.


2 Answers

With Gcc 4.8.2 I managed to link the code without errors with the following:

$ g++ -c main.cpp -O3 -flto
$ g++ -c UndefErr.cpp -O3 -flto
$ g++ main.o UndefErr.o -flto -O3 -o out

I know -flto will make the linker behave as if -fwhole-program was passed and the whole thing was a single compilation unit. And -fwhole-program, according to the manual, corresponds to the proper usage of static on functions, so allowing the unused function to be eliminated from the output (i.e. you assure the compiler all your functions will not be used by some other code, possibly dynamically loaded, and the only entry point you guarantee for your users is main()).

I had to add -O3, I am not sure why exactly, but the compiler was not very interested in inspecting the functions and eliminating dead code without it.

like image 72
lvella Avatar answered Oct 25 '22 21:10

lvella


The problem is that your undefined function is used. The object code for Func2 has been generated, there's a reference to unexisting UndefFunc, and it goes to the linker. Compiler has no way to understand that this function will always be undefined as there are multiple translation units.

The only way to avoid compilation error is to tell linker that you don't need object code for unused functions, so that it won't try to generate assembly for this case.

like image 41
polkovnikov.ph Avatar answered Oct 25 '22 19:10

polkovnikov.ph