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

标签 c++ gcc

我将以下命令行参数传递给 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/

相关文章:

c - std=gnu89 是否支持可变长度数组?

c++ - Intel 上的多线程比 AMD 慢得多

c++ - 结构和类的嵌套

c++ - 检测字段或成员getter是否为主键的技术

c++ - 如何在 visual studio 中打开 .cpp 文件并使其正确显示带有西里尔字符的字符串文字?

c++ - GCC 对 C++17 的支持情况如何?

c++ - 为什么指定的初始化程序没有在 g++ 中实现

涉及Glib2库的C程序编译问题?

gcc - assembly 中的位测试 (BT)

c++ - 我想将一个文件夹的文件变量访问到另一个文件夹的文件......我该怎么做?