Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用CMake将stb_图像包含到OpenGL项目中_Opengl_Cmake_Texture2d - Fatal编程技术网

使用CMake将stb_图像包含到OpenGL项目中

使用CMake将stb_图像包含到OpenGL项目中,opengl,cmake,texture2d,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包含到我的项目文件中。代码编译时没有错误。现在,我尝试

我需要用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_图像的教程。该纹理位于我的项目文件的同一目录中,也与stb_映像位于同一目录中。c.

在我看来,您试图链接的stb_映像版本不是为64位系统编译的。你确定你正在为你的系统使用正确的库文件吗?

嘿,你只需要将它添加到include之上

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h

这个答案帮助了我很多…在它工作之后,我阅读了stb_image.h头文件。在顶部,有一个定义STBGIMAGE实现的方向,定义STB2IMAGE实现,然后将该文件包含在一个C或C++文件中以创建实现。i、 e.应该是这样的:#包括#包括#包括#定义机顶盒映像#实现#包括“机顶盒映像.h”