包含 <iostream 时出现“未定义引用”错误>

"Undefined reference" errors when including <iostream>

提问人:18MegaMind81 提问时间:6/19/2018 更新时间:6/19/2018 访问量:641

问:

我已经使用 Cygwin 编译器和 g++ 7.3.0 在 Code::Blocks 17.12 中启动了一个新的控制台应用程序 (C++),我只有“main.cpp”,它有一个非常小的代码:

#include<iostream>
using namespace std;
int main(){
cout<<"test";
return 0;
}

我点击了构建并得到了 16 个错误,这是构建日志:

-------------- Build: Debug in 1 (compiler: Cygwin g++)---------------

g++.exe -Wall -fexceptions -g -std=c++14 -IC:\cygwin\usr\include -c C:\Users\Windows7\Desktop\BASIL\1\main.cpp -o obj\Debug\main.o
mingw32-g++.exe -LC:\cygwin\lib -o bin\Debug\1.exe obj\Debug\main.o   
obj\Debug\main.o: In function `_static_initialization_and_destruction_0':
/usr/lib/gcc/i686-pc-cygwin/7.3.0/include/c++/iostream:74: undefined reference to `__dso_handle'
/usr/lib/gcc/i686-pc-cygwin/7.3.0/include/c++/iostream:74: undefined reference to `__cxa_atexit'
C:\cygwin\lib/libpthread.a(t-d001702.o):fake:(.text+0x2): undefined reference to `_imp__pthread_mutex_destroy'
C:\cygwin\lib/libpthread.a(t-d001704.o):fake:(.text+0x2): undefined reference to `_imp__pthread_mutex_init'
C:\cygwin\lib/libpthread.a(t-d001709.o):fake:(.text+0x2): undefined reference to `_imp__pthread_mutex_unlock'
C:\cygwin\lib/libpthread.a(t-d001720.o):fake:(.text+0x2): undefined reference to `_imp__pthread_once'
C:\cygwin\lib/libpthread.a(t-d001705.o):fake:(.text+0x2): undefined reference to `_imp__pthread_mutex_lock'
C:\cygwin\lib/libpthread.a(t-d001699.o):fake:(.text+0x2): undefined reference to `_imp__pthread_key_create'
C:\cygwin\lib/libpthread.a(t-d001697.o):fake:(.text+0x2): undefined reference to `_imp__pthread_getspecific'
C:\cygwin\lib/libpthread.a(t-d001741.o):fake:(.text+0x2): undefined reference to `_imp__pthread_setspecific'
C:\cygwin\lib/libpthread.a(t-d001679.o):fake:(.text+0x2): undefined reference to `_imp__pthread_cond_wait'
C:\cygwin\lib/libpthread.a(t-d001674.o):fake:(.text+0x2): undefined reference to `_imp__pthread_cond_broadcast'
C:\cygwin\lib/libpthread.a(t-d001700.o):fake:(.text+0x2): undefined reference to `_imp__pthread_key_delete'
C:\cygwin\lib/libpthread.a(t-d001675.o):fake:(.text+0x2): undefined reference to `_imp__pthread_cond_destroy'
C:\cygwin\lib/libpthread.a(t-d001677.o):fake:(.text+0x2): undefined reference to `_imp__pthread_cond_signal'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 3 second(s))
16 error(s), 0 warning(s) (0 minute(s), 3 second(s))

我还根据文档编辑了搜索目录选项卡中的编译器、链接器和资源编译器,但没有奏效。 请帮帮我! 谢谢

C G++ Cygwin 代码块 未定义引用

评论

3赞 R Sahu 6/19/2018
该问题很可能是由于使用 for 编译和链接引起的。g++.exemingw32-g++.exe
0赞 user4581301 6/19/2018
可能需要链接 pthread 支持。Code::Blocks 的路径不够,无法知道允许所需库的配置窗格的路径-lpthread
0赞 18MegaMind81 6/19/2018
我认为这是一个 MinGw 问题,我尝试了 Cygwin 并且它正在工作,非常感谢大家

答:

-1赞 18MegaMind81 6/19/2018 #1

终于修好了!只是给仍在寻找的人答案, 只需安装 Cygwin 并转到 code::blocks 并选择 Cygwin GCC 而不是 GNU GCC 编译器,转到工具链可执行文件并将“编译器的安装目录”更改为您的 Cygwin 目录,并将“程序文件”更改为您的 cygwin 程序(如果它们尚未修改),编译器窗口应类似于附加的屏幕截图settings -> compiler

评论

0赞 6/21/2018
接受你的答案。