문제

I'm trying to compile and link some C++ code using CLang, with the following command line:

clang.exe -nostdinc -MD -fno-use-cxa-atexit -fno-rtti -fno-exceptions -fsigned-char -fno-stack-protector -fPIC -m64 -Wall -Werror -Wno-unused-function -Wno-unused-label -Wno-ignored-attributes

(I left out the output, includes, defines, etc. These are fine)

The issue I'm having is that during linking, I get the error

error: L0039: reference to undefined symbol `atexit'

I've spent quite some time on this issue already, but can't for the life of me seem to figure out how to properly resolve this.

My research so far has shown that atexit is defined in stdlib.h, but I can not use the standard library in this situation (this isn't my decision either, and is completely mandatory for this particular project).

As far as I can tell this issue is only now arising due to the fact that we now suddenly have static objects in our code which is compiled this way, which leads the compiler trying to register con/destructors to be executed for these objects, which requires a call to __cxa_atexit (which we've disabled because it was also giving undefined reference errors because of the same reason) or atexit.

I've also tried defining an arbitrary atexit function in my code, but apparently the linker doesn't want to have anything to do with this (which does seem rather strange to me).

So the question I have is: How can I get the linker to not whine about atexit, while not having to include the standard library?

Thanks a bunch in advance!

도움이 되었습니까?

해결책

For future visitors, user damvac was able to help out!

Here's my comment about the issue being resolved:

It seems I forgot to add extern "C" to the definition of atexit (I only added it to the declaration), this seems to have resolved the issue! Thanks everyone who replied, and thank you davmac for providing the solution!

다른 팁

atexit (and the whole concept of "exiting" in general) do not exist in a freestanding environment, so you can't call it in your code.

To get the compiler to not generate support code that calls it, you need to compile all your source files with -ffreestanding to specify that you are using/generating freestanding code.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top