C++ 中的 OpenGL - 链接 GLEW 和 GLFW 时出错(在 macOS 上使用 Xcode)

OpenGL in C++ - Error Linking GLEW and GLFW (Using Xcode on macOS)

提问人:sirdippingsauce 提问时间:1/20/2021 更新时间:1/20/2021 访问量:365

问:

我正在学习如何使用 Xcode 使用 C++ 在 OpenGL 中编程,并且我一直遇到链接问题。我使用的是 macOS Catalina 10.15.4。

我一直在寻找修复方法,但到目前为止没有任何效果。我三重检查了我的标题和库搜索路径,我确保包含必要的文件,所有这些。我怀疑这可能是一些奇怪的 32 位/64 位问题,但这绝对超出了我的知识范围,所以我不知道如何解决这个问题。

我已经包含了我的代码、错误,以及我的文件和 Xcode 搜索路径的一些图片。如果我没有包含某些内容或格式不正确,我深表歉意;我似乎永远无法在这个网站上正确地提出我的问题。

#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <stdlib.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
#define numVAOs 1
using namespace std;

GLuint renderingProgram;
GLuint vao[numVAOs];

GLuint createShaderProgram() {
    const char *vshaderSource = "#version410 \n" "void main(void) \n" "{gl_Position = vec4(0.0, 0.0, 0.0, 1.0);}";
    
    const char *fshaderSource = "#version410 \n" "out vec4 color; \n" "void main(void) \n" "{color = vec4(0.0, 0.0, 1.0, 1.0);}";
    
    GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
    GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(vShader, 1, &vshaderSource, NULL);
    glShaderSource(fShader, 1, &fshaderSource, NULL);
    glCompileShader(vShader);
    glCompileShader(fShader);

    GLuint vfProgram = glCreateProgram();
    glAttachShader(vfProgram, vShader);
    glAttachShader(vfProgram, fShader);
    glLinkProgram(vfProgram);
    
    return vfProgram;
}

void init(GLFWwindow* window) {
    renderingProgram = createShaderProgram();
    glGenVertexArrays(numVAOs, vao);
    glBindVertexArray(vao[0]);
}

void display(GLFWwindow* window, double currentTime) {
    glUseProgram(renderingProgram);
    glDrawArrays(GL_POINTS, 0, 1);
}

int main(void) {
    if (!glfwInit()) {exit(EXIT_FAILURE);}
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    GLFWwindow* window = glfwCreateWindow(600, 600, "Chapter2 - program1", NULL, NULL);
    int actualScreenWidth, actualScreenHeight;
    glfwGetFramebufferSize(window, &actualScreenWidth, &actualScreenHeight);
    glfwMakeContextCurrent(window);
    glViewport(0, 0, actualScreenWidth, actualScreenHeight);
    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK) {exit(EXIT_FAILURE);}
    glfwSwapInterval(1);
    init(window);
    while (!glfwWindowShouldClose(window)) {
        display(window, glfwGetTime());
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwDestroyWindow(window);
    glfwTerminate();
    exit(EXIT_SUCCESS);
}
Undefined symbols for architecture x86_64:
  "___glewAttachShader", referenced from:
      createShaderProgram() in main-1c69e8.o
  "___glewBindVertexArray", referenced from:
      init(GLFWwindow*) in main-1c69e8.o
  "___glewCompileShader", referenced from:
      createShaderProgram() in main-1c69e8.o
  "___glewCreateProgram", referenced from:
      createShaderProgram() in main-1c69e8.o
  "___glewCreateShader", referenced from:
      createShaderProgram() in main-1c69e8.o
  "___glewGenVertexArrays", referenced from:
      init(GLFWwindow*) in main-1c69e8.o
  "___glewLinkProgram", referenced from:
      createShaderProgram() in main-1c69e8.o
  "___glewShaderSource", referenced from:
      createShaderProgram() in main-1c69e8.o
  "___glewUseProgram", referenced from:
      display(GLFWwindow*, double) in main-1c69e8.o
  "_glDrawArrays", referenced from:
      display(GLFWwindow*, double) in main-1c69e8.o
  "_glViewport", referenced from:
      _main in main-1c69e8.o
  "_glewExperimental", referenced from:
      _main in main-1c69e8.o
  "_glewInit", referenced from:
      _main in main-1c69e8.o
  "_glfwCreateWindow", referenced from:
      _main in main-1c69e8.o
  "_glfwDestroyWindow", referenced from:
      _main in main-1c69e8.o
  "_glfwGetFramebufferSize", referenced from:
      _main in main-1c69e8.o
  "_glfwGetTime", referenced from:
      _main in main-1c69e8.o
  "_glfwInit", referenced from:
      _main in main-1c69e8.o
  "_glfwMakeContextCurrent", referenced from:
      _main in main-1c69e8.o
  "_glfwPollEvents", referenced from:
      _main in main-1c69e8.o
  "_glfwSwapBuffers", referenced from:
      _main in main-1c69e8.o
  "_glfwSwapInterval", referenced from:
      _main in main-1c69e8.o
  "_glfwTerminate", referenced from:
      _main in main-1c69e8.o
  "_glfwWindowHint", referenced from:
      _main in main-1c69e8.o
  "_glfwWindowShouldClose", referenced from:
      _main in main-1c69e8.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

/usr/local/include/GL/glew.h

/usr/local/include/GLFW/glfw3.h

/usr/local/lib/libGLEW.2.2.0.dylib AND /usr/local/lib/libglfw.3.3.dylib

Xcode Link Binary with Libraries

Xcode Search Paths

C++ Xcode macOS OpenGL 链接器错误

评论

0赞 David Sullivan 1/21/2021
我尽可能地遵循您的配置,它为我构建。我知道这没什么用,但你似乎很亲近
0赞 gipouf 5/3/2022
你最终解决了吗?

答: 暂无答案