C++ 独立功能

C++ freestanding features

我可以在 C++ 独立环境中使用哪些功能?我正在开发一个小内核(为了我自己的乐趣),我知道我不能使用整个 stdlib 库,但还有什么?当我尝试使用 new 和 delete 运算符时,它编译时没有遇到任何问题,但是 linker 说

undefined reference to `operator new[](unsigned long)
undefined reference to `operator delete[](void*)'

我 link 有 -lgcc-lsupc++ 选项。我知道异常处理在独立模式下是禁用的,但我有点惊讶 new 和 delete 也是。那我能用什么不能用呢?

What are the features that I can use in c++ freestanding environment?

许多独立的实现是实现定义的:

[intro.compliance] ... A freestanding implementation is one in which execution may take place without the benefit of an operating system, and has an implementation-defined set of libraries that includes certain language-support libraries

[intro.multithread] ... Under a freestanding implementation, it is implementation-defined whether a program can have more than one thread of execution.

[basic.start.main] It is implementation-defined whether a program in a freestanding environment is required to define a main function. [ Note: In a freestanding environment, start-up and termination is implementation-defined; start- up contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. — end note ]

[using.headers] C ++ headers for freestanding implementations

<ciso646>
<cstddef>
<cfloat>
<limits>
<climits>
<cstdint>
<cstdlib>
<new>
<typeinfo>
<exception>
<initializer_list>
<cstdalign>
<cstdarg>
<cstdbool>
<atomic>

[compliance] The supplied version of the header <cstdlib> shall declare at least the functions abort, atexit, at_quick_- exit, exit, and quick_exit (18.5). The other headers listed in this table shall meet the same requirements as for a hosted implementation.

请注意,malloc/free未列在<cstdlib>所需的函数中。


就您的链接器错误而言,既不需要独立实现也不需要托管实现来提供这些重载:

[replacement.functions] A C ++ program may provide the definition for any of twelve dynamic memory allocation function signatures declared in header <new>

实际上,由于独立环境不能依赖 OS,并且 malloc 通常使用 OS 提供的功能来实现,因此不太可能有免费存储独立环境中的内存管理功能。相反,托管环境需要自由存储内存管理来实现标准库的功能。