首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++,链接器问题返回未定义的引用

C++,链接器问题返回未定义的引用
EN

Stack Overflow用户
提问于 2016-10-24 05:46:42
回答 1查看 76关注 0票数 0

这可能是重复的,但我看到的所有问题的答案都没有充分回答我的问题,所以我无论如何都在发帖。

我的代码就是这样。非常基本。从另一个网站复制和粘贴。

代码语言:javascript
复制
#include <GL/glew.h> // include GLEW and new version of GL on Windows
#include <GLFW/glfw3.h> // GLFW helper library
#include <stdio.h>

int main() {
  // start GL context and O/S window using the GLFW helper library
  if (!glfwInit()) {
    fprintf(stderr, "ERROR: could not start GLFW3\n");
    return 1;
  }

  // uncomment these lines if on Apple OS X
  /*glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);*/

  GLFWwindow* window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);
  if (!window) {
    fprintf(stderr, "ERROR: could not open window with GLFW3\n");
    glfwTerminate();
    return 1;
  }
  glfwMakeContextCurrent(window);

  // start GLEW extension handler
  glewExperimental = GL_TRUE;
  glewInit();

  // get version info
  const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
  const GLubyte* version = glGetString(GL_VERSION); // version as a string
  printf("Renderer: %s\n", renderer);
  printf("OpenGL version supported %s\n", version);

  // tell GL to only draw onto a pixel if the shape is closer to the viewer
  glEnable(GL_DEPTH_TEST); // enable depth-testing
  glDepthFunc(GL_LESS); // depth-testing interprets a smaller value as "closer"

  /* OTHER STUFF GOES HERE NEXT */

  // close GL context and any other GLFW resources
  glfwTerminate();
  return 0;
}

我用的是CodeBlocks。在“包括”文件夹中,我有这些文件的目录(GL/GLFW)。如您所见,它们都包含在代码的顶部。

但是,这会返回“未定义引用”错误的长列表。我见过有人提到链接器,并提供了一长串代码,但没有解释把它放在哪里。我不知道链接器是什么,或者更确切地说,我不知道我必须想出什么代码,或者把它放在哪里才能工作。

如果有明确的解释和实际的援助,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-24 06:52:59

将C++源代码转换为可执行程序的过程由多个步骤组成:

  • 预处理
  • 编译
  • 拼装
  • 链接

但是,通常您只运行g++,然后运行不同的程序,每个步骤执行一个步骤。这就是为什么您可以在g++命令行中指定链接器的参数,它们将简单地传递到链接器上。

因此,针对您的问题:您需要链接包含符号(函数、方法等)的库。你想用的。在没有看到错误消息的情况下,哪些库和它们所在的位置是不同的。但是由于您包含GL头,您很可能需要链接一些类似libgl的东西..。

链接器参数是-L,用于指定库所在的位置(=目录),以及用于链接库的-l。库名中的“lib”和库文件后缀(.so,.a)可以省略,例如-lpthread链接库libpthread.so。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40211457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档