问题描述
我对 C++ 很陌生,我正在尝试将 opengl 与 glfw 一起使用。我在 wsl 上使用 ubuntu 18。我的包含目录中有 GL 和 GLFW,并且已将其包含在我的代码中。
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
using namespace glm;
int main(){
glfwWindowHint(GLFW_SAMPLES,4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MInor,3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL
GLFWwindow* window; // (In the accompanying source code,this variable is global for simplicity)
window = glfwCreateWindow( 1024,768,"Tutorial 01",NULL,NULL);
if( window == NULL ){
fprintf( stderr,"Hi\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental=true; // Needed in core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr,"Failed to initialize GLEW\n");
return -1;
}
}
我正在使用 g++ 来编译程序。当我使用 g++ -c main.cpp 它编译但当我尝试运行程序时抛出一个二进制错误,当我使用 g++ main.cpp -IGL -IGLFW -Iglm 我得到那个错误。我错过了什么吗?
错误:
/usr/bin/ld: /tmp/cco9YYTw.o: in function `main':
main.cpp:(.text+0x17): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x26): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x35): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x44): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x53): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text+0x74): undefined reference to `glfwCreateWindow'
/usr/bin/ld: main.cpp:(.text+0xa4): undefined reference to `glfwTerminate'
/usr/bin/ld: main.cpp:(.text+0xb7): undefined reference to `glfwMakeContextCurrent'
/usr/bin/ld: main.cpp:(.text+0xbd): undefined reference to `glewExperimental'
/usr/bin/ld: main.cpp:(.text+0xc3): undefined reference to `glewInit'
collect2: error: ld returned 1 exit status
解决方法
一个非常快速的谷歌搜索可能会有所帮助:glfw-errors-with-glfwwindowhint
链接到 -lglfw3
而不是 -lglfw
- 如果这是正确的,请投票给链接到的答案而不是这个
要覆盖图书馆的位置,从compile-glfw-on-ubuntu-and-fix-libglfw-so-cannot-open-error
这是因为 GLFW 安装程序将 libglfw.so 放在 /usr/local/lib 而不是像 Ubuntu 期望的 /usr/lib 。但是 /usr/local/lib 很好 为大多数 Unix 世界放置共享库的地方,所以让我们 只需告诉 Ubuntu 在链接我们的程序时查看那里。你会想要 编辑 /etc/ld.so.conf 并将“/usr/local/lib”行添加到文件中, 然后保存。之后,您应该以 root 身份运行 ldconfig。就像是 这个:
sudo vim /etc/ld.so.conf
# Add the path and save - then try to build again
,
要构建您的示例,您应该使用 apt-get 在您的 Ubuntu 上安装以下依赖项
- libglm-dev、libglfw-dev、libglew-dev、cmake
我已经使用 CMake 来构建您的文件
将以下 CMakeLists.txt
文件放在 .cpp 文件所在的文件夹中(我将您的代码复制到名为 glfw_sample.cpp 的文件中)
cmake_minimum_required(VERSION 3.10)
project(glfw_sample)
find_package(GLEW REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
add_executable(${PROJECT_NAME} glfw_sample.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE ${GLFW_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE GLEW::GLEW ${GLFW_LIBRARIES})
然后在shell中执行
cmake -S. -Bbuild
cmake --build build
如果两个步骤都没有发生错误,您将在 build 文件夹中找到 glfw_sample 可执行文件。
我使用以下链接来构建您的代码: