gpt4 book ai didi

gcc - g++、静态初始化和 -nostdlib

转载 作者:行者123 更新时间:2023-12-04 01:02:50 26 4
gpt4 key购买 nike

编译/链接 -nostdlib似乎阻止静态初始化,即使我添加了自己的 crti.s 和 crtn.s .init/.fini部分。

是否有使 g++ 生成插入在 .init 中的静态初始化代码的变通方法?或者我可以手动调用?

这是我尝试过的:

g++ -o test.o -c -fno-use-cxa-atexit test.cc  # has _start (entry point) 
# that calls _init and _main
as -o crti.o crti.s # has _init in section .init
as -o crtn.o crtn.s
g++ -o test ./crti.o test.o -nodefaultlibs -nostartfiles ./crtn.o
-nodefaultlibs单独包括静态初始化代码和调用,但强制使用 libc-_start/_init。
-nodefaultlibs -nostartfiles允许我使用自己的 _start/_init,但不包括代码或对静态初始化的调用。

最佳答案

来自 gcc linker docs ,

-nostdlib

Do not use the standard system startup files or libraries when linking. No startup files and only the libraries you specify will be passed to the linker, and options specifying linkage of the system libraries, such as -static-libgcc or -shared-libgcc, are ignored.



因此使用,

-nodefaultlibs

链接时不要使用标准系统库。 只有您指定的库才会传递给链接器 ,指定系统库链接的选项,例如 -static-libgcc 或 -shared-libgcc,将被忽略。 通常使用标准启动文件,除非使用 -nostartfiles .编译器可能会生成对 memcmp、memset、memcpy 和 memmove 的调用。这些条目通常由 libc 中的条目解析。指定此选项时,应通过其他某种机制提供这些入口点。

也试试,

g++ -Wl, -static

-Wl passes the next command on to the linker
-static On systems that support dynamic linking, this prevents linking with
the shared libraries. On other systems, this option has no effect.

关于gcc - g++、静态初始化和 -nostdlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10119520/

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