我正在尝试使用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。
我将继续寻找解决方案,但也许有人可以在这里帮助我
谢谢。
答案 0 :(得分:2)
这是我的GLFW,GLAD,CMAKE和QtCreator的文件夹结构。
首先从回购中安装GLFW和依赖项:
sudo apt install cmake xorg-dev libglu1-mesa-dev libglfw3-dev libglfw3
OR
要构建
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;
}