【问题标题】:Use a library and header files in Gnome Builder在 Gnome Builder 中使用库和头文件
【发布时间】:2020-02-28 19:36:17
【问题描述】:

更新我切换到介子构建系统。现在一切正常!

我对使用 C++、OpenGl 和 Gnome Builder 非常陌生。我对 C++ 有非常非常基本的基础,并且我知道如何在 CodeLite 中链接头文件和库,但是在弄乱 Gnome Builder 之后,我想进行切换。我还没有找到任何关于使用 Builder 的初学者友好教程。我只是不知道应该如何在 Builder 中链接外部库。我只是手动编辑 Makefile 还是在某个地方设置了可以使用 automake 自动执行 makefile 过程的设置?假设这是一个makefile问题,我错了吗?抱歉,如果这是一个非常新手的问题。

我正在使用 Ubuntu。对于所有 glfw 和 glew 变量和标头,我收到错误“未定义的引用 ...”。用 apt 安装库后,我的库安装在 usr/lib/x86-64-linux-gnu 中,头文件安装在 usr/include 中。

#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>


int main ()
{
  glewExperimental = true;
  if (!glfwInit() )
  {
    fprintf(stderr, "Failed to initialize GLFW \n");
    return -1;
  }
  glfwWindowHint(GLFW_SAMPLES, 4);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

  GLFWwindow* window;
  window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL);
  if ( window == NULL )
  {
    fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. \n");
    glfwTerminate();
    return -1;
  }
  glfwMakeContextCurrent(window);
  glewExperimental = true;
  if (glewInit() != GLEW_OK)
  {
    fprintf(stderr, "Failed to Initialize GLEW. \n");
    return -1;
  }

  glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

  do {
    glClear(GL_COLOR_BUFFER_BIT);
    glfwSwapBuffers(window);
    glfwPollEvents();
  }
  while ( glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0);

 return 0;
}

在尝试构建时,我得到了这个错误输出~

g++ -o practice -Wall -ggdb -fno-omit-frame-pointer -O2 practice.cpp /usr/bin/ld: /tmp/ccLx11Ky.o: 在函数main': /home/joe/Projects/practice/practice.cpp:30: undefined reference toglewExperimental' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:31: 未定义引用glfwInit' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:36: undefined reference toglfwWindowHint' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:37: 未定义引用glfwWindowHint' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:38: undefined reference toglfwWindowHint' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:39: 未定义引用glfwWindowHint' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:40: undefined reference toglfwWindowHint' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:43: 未定义对glfwCreateWindow' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:50: undefined reference toglfwMakeContextCurrent' 的引用 /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:51: 未定义引用glewExperimental' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:52: undefined reference toglewInit' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:58: 未定义引用glfwSetInputMode' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:65: undefined reference toglfwWindowShouldClose' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:61: 未定义引用glClear' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:62: undefined reference toglfwSwapBuffers' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:63: 未定义引用glfwPollEvents' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:65: undefined reference toglfwGetKey' /usr/bin/ld: /home/joe/Projects/practice/practice.cpp:47: 未定义对“glfwTerminate”的引用 collect2:错误:ld 返回 1 个退出状态 make: *** [Makefile:8: practice] 错误 1

我的默认 Makefile 如下所示~

all: practice

WARNINGS = -Wall
DEBUG = -ggdb -fno-omit-frame-pointer
OPTIMIZE = -O2

practice: Makefile practice.cpp
    $(CXX) -o $@ $(WARNINGS) $(DEBUG) $(OPTIMIZE) practice.cpp

clean:
    rm -f practice

# Builder will call this to install the application before running.
install:
    echo "Installing is not supported"

# Builder uses this target to run your application.
run:
    ./practice

【问题讨论】:

    标签: c++ ubuntu makefile gnome-builder


    【解决方案1】:

    您需要自定义构建命令(练习)以包含所需的库。考虑使用pkg-config --libs glew(或pkg-config --libs -static glew)查找需要哪些库,使用pkg-config --cflags 查找命令行标志(如果有)。

    最有可能:

    • 将“-lglfw3”添加到“练习”构建命令中,
    • 如果您使用静态库,请添加“-DGLEW_STATIC”

    glfw Errors with glfwWindowHintGLEW Linker Errors (undefined reference to `__glewBindVertexArray')

    【讨论】:

    • 谢谢。你熟悉 Gnome Builder 吗?我想知道是否有特定于 IDE 的方式来添加库。如果没有,我将直接编辑 Makefile。
    猜你喜欢
    • 2018-02-04
    • 2020-02-08
    • 2018-01-04
    • 2023-03-17
    • 2018-07-30
    • 1970-01-01
    • 2019-06-26
    • 2019-10-06
    • 1970-01-01
    相关资源
    最近更新 更多