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

时间:2015-12-12 06:28:56

标签: 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上的GCC 4.9.3版(Cygwin)

1 个答案:

答案 0 :(得分:1)

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

据我了解,您需要告诉gcc不要与运行时库-nostdlibs和标准库-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.)