nothings/stb库:undefined reference to stbi_set_flip_vertically_on_load 或 stbi_image_free

库: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]$
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值