gpt4 book ai didi

C++ 独立功能

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:27:04 25 4
gpt4 key购买 nike

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

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

我链接了 -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>

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

关于C++ 独立功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42441683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com