图书馆链接的顺序是否重要?

时间:2016-03-06 05:56:45

标签: c++ opengl makefile x11

我用两个不同的编译器选项编译main.cpp一个是好的,另一个给我错误。

区别仅在于链接-lX11的库的顺序。在一个方面,它位于第一行,另一个位于第二行:

# error
LIBS = -lglut -lGL -lGLU -lGLEW -lm -lX11 -lglfw3 -lpthread
LIBS += -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -lGL -ldl


# ok
LIBS = -lglut -lGL -lGLU -lGLEW -lm -lglfw3 -lpthread
LIBS += -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -lGL -ldl

我在ubuntu上运行此代码:

uname -a
x86_64 x86_64 x86_64 GNU/Linux

错误讯息:

/usr/bin/ld: //usr/local/lib/libglfw3.a(x11_window.c.o): undefined reference to symbol 'XConvertSelection'
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/libX11.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: *** [main] Error 1

的main.cpp

#include <iostream>

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

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


// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);

// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;

// The MAIN function, from here we start the application and run the game loop
int main()
{
    std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
    // Init GLFW
    glfwInit();
    // Set all the required options for GLFW
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    // Create a GLFWwindow object that we can use for GLFW's functions
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);
    glfwMakeContextCurrent(window);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }

    // Set the required callback functions
    glfwSetKeyCallback(window, key_callback);

    // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
    glewExperimental = GL_TRUE;
    // Initialize GLEW to setup the OpenGL Function pointers
    if (glewInit() != GLEW_OK)
    {
        std::cout << "Failed to initialize GLEW" << std::endl;
        return -1;
    }    

    // Define the viewport dimensions
    glViewport(0, 0, WIDTH, HEIGHT);

    // Game loop
    while (!glfwWindowShouldClose(window))
    {
        // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
        glfwPollEvents();

        // Render
        // Clear the colorbuffer
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // Swap the screen buffers
        glfwSwapBuffers(window);
    }

    // Terminate GLFW, clearing any resources allocated by GLFW.
    glfwTerminate();
    return 0;
}

// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
    std::cout << key << std::endl;
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}

Makefile 1 // ok

FLAGS = -Wall
CC = g++ -std=c++11
CFLAGS = $(FLAGS) $(INCLUDE)
LIBS = -lglut -lGL -lGLU -lGLEW -lm -lglfw3 -lpthread
LIBS += -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -lGL -ldl

All: main                             # change your_app.

main: main.o
    g++ -std=c++11 -o main main.o $(FLAGS) $(LIBS) 

main.o:
    g++ -std=c++11 -c -o main.o main.cpp $(LIBS) 

clean:
    rm main
    rm main.o

Makefile 2 //错误

FLAGS = -Wall
CC = g++ -std=c++11
CFLAGS = $(FLAGS) $(INCLUDE)
LIBS = -lglut -lGL -lGLU -lGLEW -lm -lX11 -lglfw3 -lpthread
LIBS += -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -lGL -ldl

All: main                             # change your_app.

main: main.o
    g++ -std=c++11 -o main main.o $(FLAGS) $(LIBS) 

main.o:
    g++ -std=c++11 -c -o main.o main.cpp $(LIBS) 

clean:
    rm main
    rm main.o

0 个答案:

没有答案