无法在docker容器内编译opengl程序

问题描述 投票:0回答:1

我想在docker容器内编译并运行opengl程序。为此我用了这个 https://github.com/atinfinity/cudagl 创建 docker 镜像并运行效果很好。 glxgears 在窗口中显示齿轮。 我还在 docker 容器内安装了 libglew-dev sudo apt-get install libglew-dev 但是当我想编译我的程序时,我收到此错误:

square.cpp:(.text+0xe): undefined reference to `glMatrixMode'
/usr/bin/ld: square.cpp:(.text+0x13): undefined reference to `glLoadIdentity'
/usr/bin/ld: square.cpp:(.text+0x32): undefined reference to `glClearColor'
/usr/bin/ld: /tmp/ccSFc4Tx.o: in function `Update(GLFWwindow*)':
square.cpp:(.text+0x60): undefined reference to `glfwSetWindowShouldClose'
/usr/bin/ld: /tmp/ccSFc4Tx.o: in function `RenderScene(GLFWwindow*)':
square.cpp:(.text+0xd1): undefined reference to `glClear'
/usr/bin/ld: square.cpp:(.text+0xf0): undefined reference to `glColor3f'
/usr/bin/ld: square.cpp:(.text+0x119): undefined reference to `glScalef'
/usr/bin/ld: square.cpp:(.text+0x176): undefined reference to `glBegin'
/usr/bin/ld: square.cpp:(.text+0x1a3): undefined reference to `glVertex2f'
/usr/bin/ld: square.cpp:(.text+0x1c8): undefined reference to `glVertex2f'
/usr/bin/ld: square.cpp:(.text+0x1f5): undefined reference to `glVertex2f'
/usr/bin/ld: square.cpp:(.text+0x1fa): undefined reference to `glEnd'
/usr/bin/ld: square.cpp:(.text+0x1ff): undefined reference to `glFlush'
/usr/bin/ld: /tmp/ccSFc4Tx.o: in function `main':
square.cpp:(.text+0x250): undefined reference to `glfwInit'
/usr/bin/ld: square.cpp:(.text+0x275): undefined reference to `glfwCreateWindow'
/usr/bin/ld: square.cpp:(.text+0x285): undefined reference to `glfwMakeContextCurrent'
/usr/bin/ld: square.cpp:(.text+0x294): undefined reference to `glfwWindowHint'
/usr/bin/ld: square.cpp:(.text+0x2af): undefined reference to `glfwSetKeyCallback'
/usr/bin/ld: square.cpp:(.text+0x2da): undefined reference to `glfwSetTime'
/usr/bin/ld: square.cpp:(.text+0x2e6): undefined reference to `glfwSwapBuffers'
/usr/bin/ld: square.cpp:(.text+0x2eb): undefined reference to `glfwPollEvents'
/usr/bin/ld: square.cpp:(.text+0x2f7): undefined reference to `glfwWindowShouldClose'
/usr/bin/ld: square.cpp:(.text+0x30c): undefined reference to `glfwDestroyWindow'
collect2: error: ld returned 1 exit status

这就是我编译程序的方式

g++ -std=c++11 -lGL -lGLU -lGLEW -lglut -lglfw square.cpp -o square
./square

这是我的

square.cpp

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cstdlib>
#include <iostream>

static int WIDTH = 640;
static int HEIGHT = 480;
int renders = 0;
double PI = 3.1415;
double trans = 0;
double x {0};
float rotatex = 0, rotatey = 0, mousex = 0, mousey = 0;
bool dragging = false;
int keyArr[350];

static void Initialize(void) {
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClearColor(0.0, 0.0, 0.0, 1.0);
}

static void Update(GLFWwindow* window) {
    if (keyArr[GLFW_KEY_ESCAPE])
        glfwSetWindowShouldClose(window, 1);
    rotatex += keyArr[GLFW_KEY_LEFT] - keyArr[GLFW_KEY_RIGHT];
    rotatey += keyArr[GLFW_KEY_UP] - keyArr[GLFW_KEY_DOWN];
}

static void RenderScene(GLFWwindow* window) {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.5, 0.5, 0.5);
        if(renders == 0){
        glScalef(1.0f, 2.0f, 1.0f);
    }
    renders++;
    if(x > -1){
        x -= 0.01;
    }else{
        x = 1;
    }


    glBegin(GL_LINE_LOOP);
    glVertex2f(x -0.25 , 0);
    glVertex2f(x, 0.25);
    glVertex2f(x +0.25, 0);
    glEnd();
    glFlush();
}

static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
    keyArr[key] = action;
}


int main(int argc, char** argv) {
    GLFWwindow* window;

    glfwInit();
    window = glfwCreateWindow(WIDTH, HEIGHT, argv[0], NULL, NULL);
    glfwMakeContextCurrent(window);
    glfwWindowHint(GLFW_SAMPLES, 4);
    Initialize();
    glfwSetKeyCallback(window, KeyCallback);
    while (!glfwWindowShouldClose(window)) {
        Update(window);
        RenderScene(window);
        glfwSetTime(0);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwDestroyWindow(window);
    return 0;
}
c++ docker opengl glfw glad
1个回答
0
投票

项目结构:

├── config
│   └── nvidia_icd.json
├── Dockerfile
└── square.cpp

🗎

Dockerfile

FROM nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04

ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
        command-not-found \
        software-properties-common \
        coreutils \
        build-essential \
        pkg-config \
        xdg-user-dirs \
        libgl1-mesa-dev \
        freeglut3-dev \
        mesa-utils \
        libvulkan-dev \
        libglfw3-dev \
        libglew-dev \
        xvfb && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /root

构建镜像并启动容器。共享当前工作目录,以便

square.cpp
在容器中可用。

docker build -t square . && docker run -v $(pwd):/root -it square

接下来的两个命令在容器中运行。编译: g++ -std=c++11 square.cpp -o square -lGL -lGLU -lGLEW -lglut -lglfw

运行:

xvfb-run -a -s "-screen 0 1400x900x24" ./square

由于我们在 Docker 容器中运行可执行文件,因此没有可用的显示。而是使用 
Xvfb

来模拟显示器。

© www.soinside.com 2019 - 2024. All rights reserved.