首页 > 解决方案 > 包含时出现“未定义的引用”错误

问题描述

我已经使用 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++cygwincodeblocksundefined-reference

解决方案


终于修好了!只是给仍在搜索的人提供答案,只需安装 Cygwin 并转到settings -> compilercode::blocks 并选择 Cygwin GCC 而不是 GNU GCC 编译器,转到 Toolchain 可执行文件并将“编译器的安装目录”更改为 Cygwin 目录并更改将“程序文件”添加到您的 cygwin 程序(如果尚未修改),编译器窗口应该看起来像所附的屏幕截图。


推荐阅读