没有标准库的准系统 C++? [英] Barebones C++ without standard library?

查看:40
本文介绍了没有标准库的准系统 C++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

诸如 GCC 和 Clang 之类的编译器允许在没有 C++ 标准库的情况下编译 C++ 程序,例如使用 -nostdlib 命令行标志.似乎这样经常无法链接你,例如:

Compilers such as GCC and Clang allow to compile C++ programs without the C++ standard library, e.g. using the -nostdlib command line flag. It seems that such often fail to link thou, for example:

void f() noexcept { throw 42; }
int main() { f(); }

通常由于未定义的符号而无法链接,例如 __cxa_allocate_exceptiontypeinfo for int__cxa_throw__gxx_personality_v0__clang_call_terminate__cxa_begin_catchstd::terminate()

Usually fails to link due to undefined symbols like __cxa_allocate_exception, typeinfo for int, __cxa_throw, __gxx_personality_v0, __clang_call_terminate, __cxa_begin_catch, std::terminate() etc.

即使是简单的

int main() {}

链接失败

ld:警告:找不到入口符号_start;默认为 0000000000400120

ld: warning: cannot find entry symbol _start; defaulting to 0000000000400120

并在执行时被操作系统杀死.使用 -c 编译器仍然运行明显失败的链接器:

and is killed by the OS upon execution. Using -c the compiler still runs the linker which blatantly fails with:

ld: mytest(.eh_frame) 中的错误;不会创建 .eh_frame_hdr 表.

ld: error in mytest(.eh_frame); no .eh_frame_hdr table will be created.

在不使用和链接到标准库的情况下编程和编译 C++ 应用程序或库是一个现实的目标吗?如何在 Linux 上使用 GCC 或 Clang 编译我的代码?如果没有标准库,将无法使用哪些核心语言功能?

Is it a realistic goal to program and compile C++ applications or libraries without using and linking to the standard library? How can I compile my code using GCC or Clang on Linux? What core language features would one be unable to use without the standard library?

推荐答案

您基本上可以在 osdev 找到所有问题的答案.org,但无论如何我都会做一个简短的总结.

You will basically find all of your questions answered at osdev.org, but I'll give a brief summary anyway.

当你给 GCC -nostdlib,您说的是没有启动文件或库文件".这包括:

When you give GCC -nostdlib, you are saying "no startup or library files". This includes:

  • crti.o, crtbegin.ocrtend.ocrtn.o.通常内核开发者只关心实现 crti.ocrtend.o 并让 GCC 提供 crtbegin.ocrtend.o 通过将 -print-file-name= 传递给链接器.通常这些只是分别由 .init.fini 组成的存根,为 GCC 留出空间来推送 crtbegin.ocrtend.o 分别.这些文件是调用全局构造函数/析构函数所必需的.
  • 您无法避免链接libgcc("低级运行时库" (-lgcc) 因为即使您传递 -nostdlib GCC 将在您使用它时发出对其函数的调用,导致看似无缘无故的莫名其妙的链接错误.即使您正在实施/移植 C 库.
  • 您不需要libstdc++",但通常内核开发人员需要它.移植 C 库然后从头开始实现 C++ 标准库是一项极其艰巨的任务.
  • crti.o, crtbegin.o, crtend.o and crtn.o. Generally kernel developers only care about implementing crti.o and crtend.o and let GCC supply crtbegin.o and crtend.o by passing -print-file-name= to the linker. Generally these are just stubs that consist of .init and .fini respectively, leaving room for GCC to shove the contents of crtbegin.o and crtend.o respectively. These files are necessary for calling global constructors/destructors.
  • You can't avoid linking libgcc (the "low-level runtime library" (-lgcc) because even if you pass -nostdlib GCC will emit calls to its functions whenever you use it, leading to inexplicable linking errors for seemingly no reason. This is the case even when you're implementing/porting a C library.
  • You don't "need" libstdc++ no, but typically kernel developers want it. Porting a C library then implementing the C++ standard library from scratch is an extremely difficult task.

由于您只想摆脱标准库",但保留 libc(在 Linux 系统上),因此您实际上只是使用 C 库对 C++ 进行编程.当然,这并没有错,你做你自己,但最终我不明白这一点,除非你计划开发一个内核.

Since you only want to get rid of the "standard library", but keeping libc (on a Linux system) you're essentially programming C++ with just a C library. Of course, there's nothing wrong with this and you do you, but ultimately I don't see the point unless you plan on developing a kernel.

必读:

OSDev 的 C++ 页面 - 如果您真的关心 RTTI/异常支持,它是 实施起来比听起来更烦人.通常,人们只是通过 -fno-rtti-fno-exceptions ,然后担心它或根本不担心.

OSDev's C++ page - If you really care about RTTI/exception support, it's more annoying to implement than it sounds. Typically people just pass -fno-rtti or -fno-exceptions and then worry about it down the line or not at all.

这篇关于没有标准库的准系统 C++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆