我需要为学生项目使用 OpenGL 编写一个小游戏。我还必须使用 CMake 来完成这个项目。我包含了用于导入纹理的 stb_image.c。
我将以下几行添加到我的 CMakeList.txt
include_directories(${CMAKE_SOURCE_DIR}/../extern/stb_image)
link_directories(${CMAKE_SOURCE_DIR}/../extern/stb_image)
并将 stb_image.h 包含到我的项目文件中。代码编译没有错误。现在我尝试使用以下代码将纹理导入到我的项目中。
#include "stb_image.h"
int x, y, n;
unsigned char *data = stbi_load("testTexture.png" , &x, &y, &n, 0);
如果我尝试使用“make”命令编译项目,则会收到以下错误:
Linking CXX executable /Users/.../Documents/.../.../.../binaries/basic-texturing
Undefined symbols for architecture x86_64:
"_stbi_load", referenced from:
loadObjectsAndTextures() in inits.cc.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [/Users/.../Documents/.../.../.../binaries/basic-texturing] Error 1
make[1]: *** [CMakeFiles/basic-texturing.dir/all] Error 2
make: *** [all] Error 2
inits.cc是项目文件,loadObjectsAndTextures()是调用stbi_load()的方法。
我真的不知道这里发生了什么崩溃。我与 CMake 的链接文件是否错误,是我的函数调用错误还是我在这里没有看到。您能给我一些建议,让我可以从哪里开始解决这个问题吗?我尝试了几个小时来阅读 CMake 和 Documentation 的教程。对于stb_image。该纹理位于我的项目文件的同一目录中,并且与 stb_image.c 位于同一目录中。
最佳答案
嘿,您只需将其添加到包含上方即可。
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h
关于opengl - 使用 CMake 将 stb_image 包含到 OpenGL 项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21393675/