【问题标题】:Can't link glfw with CMake in QtCreator in Ubuntu无法在 Ubuntu 的 QtCreator 中将 glfw 与 CMake 链接
【发布时间】:2016-02-19 11:34:34
【问题描述】:

我正在尝试使用 cmake 将 glfw 库链接到我的项目。
我在基本操作系统(Ubuntu 14.04)上使用 QtCreator 3.6。
这是我的代码:

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

int main() {

std::cout << "Hello World!" << std::endl;

if(!glfwInit()) {
    std::cout << "Error" << std::endl;
} else {
    std::cout << "Success" << std::endl;
}

glfwTerminate();

}

这是我的 CMakeLists.txt:

cmake_minimum_required (VERSION 2.8)
project (test)

add_library(glfw3 main.cpp)
include_directories("${PROJECT_SOURCE_DIR}/glfw/include")
add_subdirectory(glfw)

add_executable(test main.cpp)
target_link_libraries (test glfw ${GLFW_LIBRARIES})

此 CMake 文件的代码成功编译并通过终端 (cmake/make/./test) 运行,但是当我在 QtCreator 中编译它时,我得到一个 lot of "undefined reference" errors
我会继续寻找解决方案,但也许有人可以在这里帮助我。
谢谢。

【问题讨论】:

  • “未定义引用” - 表示对外部动态库(so、a、dll)的未定义引用。检查路径。
  • @AlexanderChernin 对不起,我不太了解你。我应该检查哪些路径?如果我可以通过终端编译我的程序,那不就意味着我所有的路径都是正确的吗?
  • qtCreator 的 pro 文件中 glfw 库的路径。
  • @AlexanderChernin 好的,好吧...我不使用 qmake,我使用 cmake。无论如何,谢谢。
  • 对不起。艰难的日子......可能是这个帮助“Qt Creator 使用 FIND_PACKAGE() 宏检测外部库。一些库随 CMake 安装一起提供。您可以在 CMake 安装的 Modules 目录中找到这些库。”从这里doc.qt.io/qtcreator/creator-project-cmake.html

标签: c++ cmake qt-creator glfw


【解决方案1】:

这是我的 GLFW、GLAD、CMAKE 和 QtCreator 的文件夹结构。

首先从 Repo 安装 GLFW 和依赖项:

sudo apt install cmake xorg-dev libglu1-mesa-dev libglfw3-dev libglfw3

构建它

version="3.2.1" && \
wget "https://github.com/glfw/glfw/releases/download/${version}/glfw-${version}.zip" && \
unzip glfw-${version}.zip && \
cd glfw-${version} && \
sudo apt-get install cmake xorg-dev libglu1-mesa-dev && \
sudo cmake -G "Unix Makefiles" && \
sudo make && \
sudo make install

你可以从https://glad.dav1d.de/下载高兴

我很高兴的项目结构:

test
  CMakeLists.txt
  source>main.cpp
  lib
    glad
      include>glad>glad.h
      src>glad.c

我的 Cmakelist:

cmake_minimum_required(VERSION 2.8)

project(test)

# Source files
set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/source")
set(SOURCES "${SRC_DIR}/main.cpp")

# Executable definition and properties
add_executable(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE "${SRC_DIR}")
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)

TARGET_LINK_LIBRARIES(test -lglfw -lGL -lX11
    -lXi -lXrandr -lXxf86vm -lXinerama -lXcursor -lrt -lm -pthread)

# glad
set(GLAD_DIR "${LIB_DIR}lib/glad")
add_library("glad" "${GLAD_DIR}/src/glad.c")
target_include_directories("glad" PRIVATE "${GLAD_DIR}/include")
target_include_directories(${PROJECT_NAME} PRIVATE "${GLAD_DIR}/include")
target_link_libraries(${PROJECT_NAME} "glad" "${CMAKE_DL_LIBS}")

测试是否一切正常:

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

#include <stdio.h>

namespace {
   void errorCallback(int error, const char* description) {
      fprintf(stderr, "GLFW error %d: %s\n", error, description);
   }

   GLFWwindow* initialize() {
      int glfwInitRes = glfwInit();
      if (!glfwInitRes) {
         fprintf(stderr, "Unable to initialize GLFW\n");
         return nullptr;
      }

      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
      glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
      glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

      GLFWwindow* window = glfwCreateWindow(1280, 720, "InitGL", nullptr, nullptr);
      if (!window) {
         fprintf(stderr, "Unable to create GLFW window\n");
         glfwTerminate();
         return nullptr;
      }

      glfwMakeContextCurrent(window);

      int gladInitRes = gladLoadGL();
      if (!gladInitRes) {
         fprintf(stderr, "Unable to initialize glad\n");
         glfwDestroyWindow(window);
         glfwTerminate();
         return nullptr;
      }

      return window;
   }
}

int main(int argc, char* argv[]) {
   glfwSetErrorCallback(errorCallback);

   GLFWwindow* window = initialize();
   if (!window) {
      return 0;
   }

   // Set the clear color to a nice green
   glClearColor(0.15f, 0.6f, 0.4f, 1.0f);

   while (!glfwWindowShouldClose(window)) {
      glClear(GL_COLOR_BUFFER_BIT);

      glfwSwapBuffers(window);
      glfwPollEvents();
   }

   glfwDestroyWindow(window);
   glfwTerminate();

   return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多