【问题标题】:I get linker errors when trying to compile my opengl project using gcc尝试使用 gcc 编译我的 opengl 项目时出现链接器错误
【发布时间】:2021-12-23 18:58:06
【问题描述】:

我正在使用 glfw 和 glew 创建窗口,它给了我链接器错误。我该如何解决。这是我第一次安装 glfw 和 glew,所以我可能错过了安装步骤。有什么帮助吗?

我只使用以下代码编译我的代码:

gcc main.c

我知道有一些链接器标志,但 glfw 和 glew 不是其中的一部分。

我使用 linux 和 gcc 编译器。

这是我的代码:

#define GLEW_STATIC
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw3.h>
#include <stdbool.h>

int main(){
   glewExperimental = true;
   if(!glfwInit()){
    printf("failed to initialize GLFW\n");
    return -1;
   }
   glfwWindowHint(GLFW_SAMPLES, 4);
   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
   glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
   glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

   GLFWwindow* window;
   window = glfwCreateWindow(1024, 768, "Game", NULL, NULL);
   if(window == NULL){
    printf("Sorry to say this, but your OpenGL version must be 3.3 or above! Thanks for playing, but to continue you must update your video card drivers or if you use an old GPU you may have to replace it with a new one to play this game. I will be developing my game for OpenGL 1 and 2 soon so stay on touch.");
    glfwTerminate();
    return -1;
   }
   glfwMakeContentCurrent(window);
   glewExperimental = true;
   if(glewInit() != GLEW_OK){
    printf("Failed to initilize GLEW");
    return -1;
   }

}

这是错误日志:

/usr/bin/ld: /tmp/cc74wuFz.o: in function `main':
main.c:(.text+0xe): undefined reference to `glewExperimental'
/usr/bin/ld: main.c:(.text+0x14): undefined reference to `glfwInit'
/usr/bin/ld: main.c:(.text+0x3d): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.c:(.text+0x4c): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.c:(.text+0x5b): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.c:(.text+0x6a): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.c:(.text+0x79): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.c:(.text+0x9a): undefined reference to `glfwCreateWindow'
/usr/bin/ld: main.c:(.text+0xbb): undefined reference to `glfwTerminate'
/usr/bin/ld: main.c:(.text+0xd3): undefined reference to `glfwMakeContentCurrent'
/usr/bin/ld: main.c:(.text+0xd9): undefined reference to `glewExperimental'
/usr/bin/ld: main.c:(.text+0xdf): undefined reference to `glewInit'
collect2: error: ld returned 1 exit status

【问题讨论】:

  • 很明显链接器找不到 OpenGL 库。因此,与其发布代码,不如描述安装时所采取的步骤。
  • 我只是将头文件放在 /usr/local/include/GL 中,这就是我所做的。请告诉我是否需要导出链接器文件
  • 我使用带有 gcc 编译器的 Linux(Linux Mint,如果这很重要的话)。我没有 IDE。

标签: c gcc glfw glew


【解决方案1】:

我建议用类似的命令编译

gcc -Wall -Wextra -g $(pkg-config --cflags opengl glfw3) main.c \
     $(pkg-config --libs opengl glfw3) -o prog

当然(在尝试之前)您需要安装 OpenGL 相关库,可能使用(作为 root)以下命令

aptitude install libopengl-dev libglfw3-dev

请务必阅读documentation of GCCdocumentation of GDBdocumentation of binutilsdocumentation of pkg-config

附言。您可以从现有开源项目的源代码中获得灵感,例如GNU emacsRefPerSys

注意。您可以通过电子邮件与我联系:basile@starynkevitch.net(家)或basile.starynkevitch@cea.fr(办公室,CEA, LIST ....)在法国巴黎附近。

【讨论】:

  • 成功了!谢谢!
  • 那么请点赞或接受我的回答。请随时给我发送电子邮件至basile@starynkevitch.net(家庭)或basile.starynkevitch@cea.fr(办公室,www-list.cea.fr ....),在法国巴黎附近
猜你喜欢
  • 2013-03-04
  • 2019-09-22
  • 1970-01-01
  • 1970-01-01
  • 2018-07-14
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
相关资源
最近更新 更多