0
votes

I've got a question regarding using one of the GCC linker options: -Wl,--defsym.

Some time ago I decided to rewrite one of my projects in C++ but without using its standard library and without even linking to it (I compile .cpp source files to object files using C++ compiler but I link them using C compiler).

For that I used following compiler flags:

-fno-exceptions -fno-rtti -nostdlib -nodefaultlibs

And following linker options:

-Wl,--defsym -Wl,__cxa_pure_virtual=0

Using those flags I got my shared library compiling and linking fine. But after I try to use my shared library in some simple program (also compiled and linked using above flags) I get following error while running it:

examples/bin/blink: symbol lookup error: examples/bin/libblink.so: undefined symbol: __cxa_pure_virtual

where blink is the name of the executable and libblink.so is the name of my shared library.

I tried to fix it and it looks like replacing --Wl,--defsym linker flag (for both executable and library) with this function:

extern "C" void __cxa_pure_virtual
{
    while (true);
}

does the job. Why is the --Wl,--defsym not working in this case? I'd also like to mention that I tested this under Windows and it works fine there.

1
Rerite one of my projects in C++ but without using its standard library and without even linking to it why on earth? Standard library is what gives C++ it's power! - SergeyA
There's plenty of reasons why one would not like his program being linked to C++ std lib. One of them would be space limit on embedded devices. - user2180248
What I do not understand is: did you used virtual function calls? How should that work without having runtime support? - user16
I did use virtual function calls and of course they will work without "runtime support". Virtual function call is just dereferencing vtable pointer so there's no need for c++ std lib to do anything. Also I changed from C to C++ just for oop (I was tired of doing oop-like code in C) and I did not want to sacrifice the binary size for that. - user2180248
A compiler does not link. There is no GNU C-linker. - too honest for this site

1 Answers

0
votes

I think that I've found an answer to my question.

Changing the symbol address from 0 to any other value fixes my issue. So instead of having:

--Wl,--defsym --Wl,__cxa_pure_virtual=0

I have:

--Wl,--defsym --Wl,__cxa_pure_virtual=1

This way runtime linker does not look for a symbol (which I think is the case when the address is set to 0).