我将以下命令行参数传递给 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
islibgcc.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 oflibgcc.a
.) In most cases, you needlibgcc.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/