Reputation: 751
I have a minimal cpp file with contents,
int main() {}
I then compile and link using
$ g++ -c main.cpp
$ g++ -o main main.o -Wl,-u,foo
Expected Behaviour: Since the linker cannot find the symbol foo, because it is not defined anywhere, I expected the linker to throw an error saying that the symbol foo
is not found/resolved.
Actual behaviour: The link step succeeds.
Can someone help me understand this behaviour and how I can force the linker to error out when it cannot find the undefined symbol, possibly using some linker flag?
$ g++ --version
g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-23)
Thanks in advance!!!
EDIT: It is probably related to my misunderstanding about undefined symbols, so let me clarify my understanding of the linker. The linker searches for undefined symbols in the list sequentially through -llib...
and foo
in this case is one such undefined symbol. I was expecting that at the end of traversing the whole list of libraries, there shouldn't be any undefined symbols left i.e. all symbols must be defined. Am I wrong in thinking this?
Upvotes: 0
Views: 151
Reputation: 179779
That's simply not what -Wl,u
does. From the ld
documentation: "Force symbol to be entered in the output file as an undefined symbol.". So the -u
creates a symbol table in your main
executable, and adds foo
to that.
I.e. the option doesn't affect whether an output file is generated.
You may want to try g++ -c main.cpp -u foo
- pass it to the compiler instead of the linker, so the compiler can put foo
in the symbol table of main.o
. Now the linker will see foo
in its inputs, and it will need to act on that.
Upvotes: 1