C++ collect2.exe [错误] ld 返回 1 个退出状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23500269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:28:09  来源:igfitidea点击:

collect2.exe [Error] ld returned 1 exit status

c++cwindows-7opengl-3glfw

提问by user3609058

I am programming since a few days and I simply want to open a window with opengl. I got glew and glfw, I linked the library of glew and the headers of both but when I compile I get an error. I've got windows7. I use def c++.

几天以来我一直在编程,我只想用opengl打开一个窗口。我得到了glew和glfw,我链接了glew库和两者的头文件,但是当我编译时出现错误。我有windows7。我使用 def C++。

This is the code:

这是代码:

#define GLEW_NO_GLU
#define GLFW_NO_GLU

#include <GL/glew.h>
#include <GLFW/glfw3.h>


int main() {
GLFWwindow* window1 = glfwCreateWindow(640, 480, "window1", NULL, NULL);
glfwMakeContextCurrent(window1);
return 0;
}

And this are the errors:

这是错误:

C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL\main.o   In function `main':
C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL\main.cpp undefined reference to `glfwCreateWindow'
C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL\main.cpp undefined reference to `glfwMakeContextCurrent'
C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL\collect2.exe [Error] ld returned 1 exit status

This is the compiler log or however it is called:

这是编译器日志,或者它被称为:

Compiler: TDM-GCC 4.7.1 32-bit Release
Building Makefile"C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL\Makefile.win"
Führe  make... aus
mingw32-make.exe -f "C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL\Makefile.win" all
g++.exe -D__DEBUG__ main.o -o Projekt2.exe -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib32" -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib32" -static-libgcc glew-1.10.0/lib/Release/Win32/glew32.lib -m32 -g3

main.o: In function `main':
C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL/main.cpp:9: undefined reference to `glfwCreateWindow'
C:\Users\Stefan\Desktop\Programmieren\Code\c++_Projekt2_OpenGL/main.cpp:10: undefined reference to `glfwMakeContextCurrent'
collect2.exe: error: ld returned 1 exit status

mingw32-make.exe: *** [Projekt2.exe] Error 1

Compilation failed after 0,14 seconds with errors

回答by Leonardo

From the log messages you posted, it seems that you haven't configured your Dev-C++ project properly to link against glfw:

从您发布的日志消息来看,您似乎没有正确配置 Dev-C++ 项目以链接到glfw

g++.exe -D__DEBUG__ main.o -o Projekt2.exe -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib32" -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib32" -static-libgcc glew-1.10.0/lib/Release/Win32/glew32.lib -m32 -g3

The glfwlibrary is missing, that is why you're getting an undefined reference to its functions. You should set up it as you did with glew32.

glfw缺少该库,这就是为什么您会收到对其函数的未定义引用。您应该像对glew32.

Also, the link to What is an undefined reference/unresolved external symbol error and how do I fix it?, provided in the comments to your question, is worth the read.

此外,链接到什么是未定义的引用/未解析的外部符号错误以及如何修复它?,在您的问题的评论中提供,值得一读。