g++中的多重声明错误(cygwin)

Multiple declaration error in g++ (cygwin)

本文关键字:错误 cygwin 声明 g++      更新时间:2023-10-16

我最近决定开始编写符合标准的C++,而不是使用Turbo-C++,所以我在Windows 7上下载了cygwin并安装了g++。我尝试运行一个hello world程序:

#include<iostream>
using namespace std;
int main()
{
    cout << "Hello World!" << endl;
    return 0;
}

并在Cygwin中键入以下内容:

g++ test.cpp -o test
g++ test

希望我没事,我按回车键,发现我的心被这堵巨大的难以理解的文字墙打破了:

$ g++ test.exe
test.exe: In function `mainCRTStartup':
/usr/src/debug/cygwin-1.7.27-2/winsup/cygwin/crt0.c:23: multiple definition of `mainCRTStartup'
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../lib/crt0.o:/usr/src/debug/cygwin-1.7.27-2/winsup/cygwin/crt0.c:23: first defined here
test.exe: In function `mainCRTStartup':
/usr/src/debug/cygwin-1.7.27-2/winsup/cygwin/crt0.c:23: multiple definition of `WinMainCRTStartup'
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../lib/crt0.o:/usr/src/debug/cygwin-1.7.27-2/winsup/cygwin/crt0.c:23: first defined here
test.exe:cygming-crtbegin.c:(.text+0x60): multiple definition of `__gcc_register_frame'
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/crtbegin.o:cygming-crtbegin.c:(.text+0x10): first defined here
test.exe:cygming-crtbegin.c:(.text+0xc0): multiple definition of `__gcc_deregister_frame'
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/crtbegin.o:cygming-crtbegin.c:(.text+0x70): first defined here
test.exe:crt0.c:(.text+0x50): multiple definition of `.weak._Jv_RegisterClasses.__gcc_register_frame'
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/crtbegin.o:cygming-crtbegin.c:(.text+0x0): first defined here
test.exe:cygming-crtend.c:(.idata+0x178): multiple definition of `__imp__ZSt4cout'
test.exe:cygming-crtend.c:(.idata+0x178): first defined here
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/crtbegin.o:cygming-crtbegin.c:(.text+0x35): undefined reference to `_Jv_RegisterClasses'
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/crtbegin.o:cygming-crtbegin.c:(.text+0x35): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `_Jv_RegisterClasses'
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../x86_64-pc-cygwin/bin/ld: /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/crtbegin.o: bad reloc address 0x0 in section `.pdata'
collect2: error: ld returned 1 exit status

有人可以看看这个,告诉我我做错了什么吗?

不需要 g++ test 命令

命令g++ test.cpp -o test将编译并链接您的代码。 您可以使用./test.exe运行它