gpt4 book ai didi

c++ - 我如何告诉 GCC 不要链接运行时库和标准库?

转载 作者:行者123 更新时间:2023-11-28 05:55:33 30 4
gpt4 key购买 nike

我将以下命令行参数传递给 GCC:

gcc -e _main -nostdlib -nodefaultlibs -fno-exceptions -nostartfiles -fno-rtti -g C:\ntdll.cpp

它给了我以下错误:

/ cygdrive / c / Users / ---- / AppData / Local / Temp / ccDrwTbB.o: In function `main':
C : \users\nabeel\desktop / ntdll.cpp:5 : undefined reference to `__main'
C : \users\nabeel\desktop / ntdll.cpp:5 : (.text + 0x9) : relocation truncated to fit : R_X86_64_PC32 against undefined symbol `__main'
collect2 : error : ld returned 1 exit status

知道为什么会这样吗?

//ntdll.cpp:
int main()
{
}

Windows (Cygwin) 上的 GCC 版本 4.9.3

最佳答案

为什么要使用 -nostartfiles 选项?使用该选项,您需要定义自己的 __start 条目来准备环境并调用 main 函数。删除选项 -nostartfiles 并重试。参见 https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

据我了解,您需要告诉 gcc 不要链接运行时库 -nostdlib 和标准库 -nodefaultlibs

One of the standard libraries bypassed by -nostdlib and -nodefaultlibs is libgcc.a, a library of internal subroutines that GCC uses to overcome shortcomings of particular machines, or special needs for some languages. (See Interfacing to GCC Output, for more discussion of libgcc.a.) In most cases, you need libgcc.a even when you want to avoid other standard libraries. In other words, when you specify -nostdlib or -nodefaultlibs you should usually specify -lgcc as well. This ensures that you have no unresolved references to internal GCC library subroutines. (For example, __main, used to ensure C++ constructors will be called; see collect2.)

关于c++ - 我如何告诉 GCC 不要链接运行时库和标准库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34237029/

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