【问题标题】:"Undefined reference" errors when including <iostream>包含 <iostream> 时出现“未定义的引用”错误
【发布时间】:2018-11-27 16:27:29
【问题描述】:

我已经使用 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))

我还根据文档在搜索目录选项卡中编辑了编译器、链接器和资源编译器,但这不起作用。 请帮助我! 谢谢,

【问题讨论】:

  • 这个问题很可能是使用g++.exe编译和mingw32-g++.exe链接造成的。
  • 可能需要-lpthread 来链接 pthread 支持。在 Code::Blocks 上还不够了解允许所需库的配置窗格的路径
  • 我认为这是一个 MinGw 问题,我尝试了 Cygwin,它正在工作,非常感谢大家

标签: c++ g++ cygwin codeblocks undefined-reference


【解决方案1】:

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

【讨论】:

  • 接受你的回答。
猜你喜欢
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
  • 2019-06-03
  • 1970-01-01
  • 2021-11-17
  • 2015-05-21
  • 2012-02-02
  • 1970-01-01
相关资源
最近更新 更多