库:https://github.com/nothings/stb
①编译出错:
[jn@jn build]$ make
[ 33%] Building CXX object CMakeFiles/glfwTest.dir/main.cpp.o
[ 66%] Building C object CMakeFiles/glfwTest.dir/src/glad.c.o
[100%] Linking CXX executable glfwTest
/usr/bin/ld: CMakeFiles/glfwTest.dir/main.cpp.o: in function main':
main.cpp:(.text+0x505): undefined reference to stbi_set_flip_vertically_on_load'
/usr/bin/ld: main.cpp:(.text+0x532): undefined reference to stbi_load'
/usr/bin/ld: main.cpp:(.text+0x5d5): undefined reference to stbi_image_free'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/glfwTest.dir/build.make:117: glfwTest] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/glfwTest.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
[jn@jn build]$
② 原因以及解决:
stb_image.h 文件本身只包含声明和函数的实现。但是需要声明STB_IMAGE_IMPLEMENTATION。以为实现都在这里
//
//
end header file /
#endif // STBI_INCLUDE_STB_IMAGE_H
#ifdef STB_IMAGE_IMPLEMENTATION
实现...
#endif // STB_IMAGE_IMPLEMENTATION
所以自己代码要声明:
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
③编译通过
[jn@jn build]$ make
Consolidate compiler generated dependencies of target glfwTest
[100%] Built target glfwTest
[jn@jn build]$ make clean
[jn@jn build]$ make
[ 33%] Building CXX object CMakeFiles/glfwTest.dir/main.cpp.o
[ 66%] Building C object CMakeFiles/glfwTest.dir/src/glad.c.o
[100%] Linking CXX executable glfwTest
[100%] Built target glfwTest
[jn@jn build]$ head ../main.cpp
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h> // 使用stb_image库加载纹理
//
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
const char* vertexShaderSource = R"(
#version 330 core
layout(location = 0) in vec3 aPos;
[jn@jn build]$