包括使用CMake的OpenGL项目的stb_image

时间:2014-01-27 23:10:41

标签: opengl cmake texture2d

我需要使用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的教程和stb_image的Documentation。纹理位于我的项目文件的同一目录中,也与stb_image.c位于同一目录中。

2 个答案:

答案 0 :(得分:0)

嘿,你只需要在include上面添加它。

定义STB_IMAGE_IMPLEMENTATION

包括“stb_image.h”

答案 1 :(得分:-1)

在我看来,你正试图链接一个没有为64位系统编译的stb_image版本。您确定要为系统使用正确的库文件吗?

相关问题