【问题标题】:OpenGL 4.1 on OSX Xcode 6 setup difficultiesOSX Xcode 6 上的 OpenGL 4.1 设置困难
【发布时间】:2015-08-02 20:16:44
【问题描述】:

我正在尝试学习一些 OpenGL,并立即发现 OpenGL >3.2 的版本更适合学习。

所以我使用 Xcode 和命令行工具设置了我的 Mac OS X 10.10.3 以获取一些示例。

但是jebus这东西真是让人头疼。

我得到的错误是。

    Undefined symbols for architecture x86_64:
  "LoadShaders(char const*, char const*)", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

所以我有最新版本的GLFW GLEW cmake 来让事情顺利进行。

我将我的头文件和库路径添加到项目中。

图书馆搜索路径

/opt/X11/lib /usr/lib/ /Users/mac/Dev/workspace/C++/Test/glfw/build/src/Debug /Users/mac/Dev/workspace/C++/Test/glew/lib
  • 我添加最后两个目录只是为了在目录上加倍

Header Search Path 看起来与 include 而不是 lib 相似

我的链接器很大(来自尝试随机的东西)

-framework OpenGl -lGLUT -lglew -lglfw -lGL -lGLU -lXmu -lXi -lXext -lX11 -lXt

我也将二进制文件与库链接起来。我哪里错了?? 我的 GPU 是 Intel HD 4000,似乎最高支持 OpenGL4.1。

我需要添加编译器标志吗?这不合理吗?

这是我正在尝试运行的教程代码。

    #include <stdio.h>
#include <stdlib.h>

//GLEW
#define GLEW_STATIC
#include <GL/glew.h>

#include <GLUT/glut.h>

//GLFW
#include <GLFW/glfw3.h>
#include <AGL/glm.h>

GLFWwindow* window;

//no real explanation of what this is.....
#include <common/shader.hpp>

//MAIN FUNCTION
int main(int argc, char *argv[])
{
    if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        return -1;
    }

    // specify GL information
    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // We want 4.1>= OpenGL_version >=3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
    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

    // Open a window and create its OpenGL context
    window = glfwCreateWindow( 600, 375, "Tutorial 02", NULL, NULL);

    if( window == NULL ){
        fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\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;
    }
    // Ensure we can capture the escape key being pressed below
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    // Dark blue background
    glClearColor(0.0f, 0.0f, 0.4f, 0.0f);

    // Create Vertex Array Object.
    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);

    // Create and compile our GLSL program from the shaders
    GLuint programID = LoadShaders( "SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader" );

    // An array of 3 vectors which represents 3 vertices
    static const GLfloat g_vertex_buffer_data[] = {
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        0.0f,  1.0f, 0.0f,
    };

    GLuint vertexbuffer;
    glGenBuffers(1, &vertexbuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

    do{
        // Clear the screen
        glClear( GL_COLOR_BUFFER_BIT );

        // Use our Shader
        glUseProgram(programID);

        // 1st attribute buffer : vertices
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

        // glVertexAttribPointer(
        //                          Atrribute,
        //                          size,
        //                          type,
        //                          normalized,
        //                          stride,
        //                          array buffer offset
        //                      );
        glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

        // Draw the triangle
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glDisableVertexAttribArray(0);

        // Swap buffers
        glfwSwapBuffers(window);
        glfwPollEvents();

    } // Check if the ESC key was pressed or the window was closed
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
          glfwWindowShouldClose(window) == 0 );

    // Cleanup VBO
    glDeleteBuffers(1, &vertexbuffer);
    glDeleteVertexArrays(1, &VertexArrayID);
    glDeleteProgram(programID);

    // Close OpenGL window and terminate GLFW
    glfwTerminate();

    return 0;
}

我在这里主要遵循这个准则 Oscar Chavez, Setup instructions

【问题讨论】:

  • 如果教程告诉您使用 cmake,请使用 cmake - 不要手动创建项目。链接到教程将有助于提供指导。

标签: c++ xcode macos opengl


【解决方案1】:

函数LoadShaders() 不是 OpenGL 的一部分,也不属于您正在使用的任何库(我看到 GLEW 和 GLFW)。总之,LoadShaders() 不见了。

我的猜测是 LoadShaders() 是教程作者写的一个函数,但是教程的格式有点令人沮丧(至少可以这么说!)所以我不确定。

【讨论】:

  • 是的,感谢您澄清这一点。我不知道,因为我是 GL 新手。
【解决方案2】:
//no real explanation of what this is.....
#include <common/shader.hpp>

可能是该教程的作者自己写的东西,只是简单地转储到项目源代码中,没有透露更多信息。关于这一行最烦人的部分是使用楔形括号 (&lt;…&gt;) 而不是引号 ("…"),因为这会误导性地表明标头是系统库的一部分,而不是项目本地的一部分。 Somwehere 可能有一个common/shader.cpp,其中可能有一个函数LoadShaders。正是这个函数,完全是自定义的,而不是 OpenGL 或任何帮助程序库的一部分,链接器告诉你出错了。

【讨论】:

  • 是的,谢谢。我也对作者编写动态库而不是静态库感到恼火。
猜你喜欢
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 2014-06-14
  • 1970-01-01
相关资源
最近更新 更多