简体   繁体   English

无法使用 stb_image

[英]Unable to use stb_image

I was working on a project and I was trying to get an image to load so I could use it as a texture, but I ran into an issue when I tried to include stb_image.我正在做一个项目,我试图加载一个图像,以便我可以将它用作纹理,但是当我尝试包含 stb_image 时遇到了一个问题。

I put stb_image.h in src/vendor/stb_image/ , made a cpp file named stb_image.cpp with我将stb_image.h放在src/vendor/stb_image/中,制作了一个名为stb_image.cpp的 cpp 文件

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

in the file, and put the file in the src/vendor/stb_image/ directory.在文件中,并将文件放在src/vendor/stb_image/目录中。 When I try to include #include "vendor/stb_image/stb_image.h" and use it, I get this error:当我尝试包含#include "vendor/stb_image/stb_image.h"并使用它时,我收到此错误:

: && /usr/bin/c++  -Wall -Werror -std=c++14 -g   CMakeFiles/opengl-learning.dir/src/Debug.cpp.o CMakeFiles/opengl-learning.dir/src/IndexBuffer.cpp.o CMakeFiles/opengl-learning.dir/src/Renderer.cpp.o CMakeFiles/opengl-learning.dir/src/Shader.cpp.o CMakeFiles/opengl-learning.dir/src/Texture.cpp.o CMakeFiles/opengl-learning.dir/src/VertexArray.cpp.o CMakeFiles/opengl-learning.dir/src/VertexBuffer.cpp.o CMakeFiles/opengl-learning.dir/src/main.cpp.o  -o opengl-learning  -lglfw /usr/lib64/libGLEW.so /usr/lib/x86_64-linux-gnu/libOpenGL.so /usr/lib/x86_64-linux-gnu/libGLX.so /usr/lib/x86_64-linux-gnu/libGLU.so && :
/usr/bin/ld: CMakeFiles/opengl-learning.dir/src/Texture.cpp.o: in function `Texture::Texture(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/home/user/Documents/CPP-Stuff/OpenGL-Learning/src/Texture.cpp:5: undefined reference to `stbi_set_flip_vertically_on_load'
/usr/bin/ld: /home/user/Documents/CPP-Stuff/OpenGL-Learning/src/Texture.cpp:6: undefined reference to `stbi_load'
/usr/bin/ld: /home/user/Documents/CPP-Stuff/OpenGL-Learning/src/Texture.cpp:23: undefined reference to `stbi_image_free'
collect2: error: ld returned 1 exit status

This is the code I am trying to run, that makes use of stb_image:这是我试图运行的代码,它使用了 stb_image:

stbi_set_flip_vertically_on_load(1);                                        
m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4);  

I've tried putting #define STB_IMAGE_IMPLEMENTATION in the file using stb_image and then including it, but that didn't work very well.我尝试使用 stb_image 将#define STB_IMAGE_IMPLEMENTATION放入文件中,然后将其包含在内,但效果不佳。 It was better than before, but when I tried loading the images, nothing happened.比以前好多了,但是当我尝试加载图像时,什么也没发生。 I printed out some of the image data with我打印了一些图像数据

stbi_set_flip_vertically_on_load(1);                                        
m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4);    
                                                              
std::cout << "m_BPP: " << m_BPP << " m_Width: " << m_Width << " m_Height: " << m_Height << std::endl;

It printed m_BPP: 0 m_Width: 0 m_Height: 0 (the default values, meaning that it didn't load anything).它打印m_BPP: 0 m_Width: 0 m_Height: 0 (默认值,意味着它没有加载任何东西)。 m_LocalBuffer was empty as well. m_LocalBuffer 也是空的。 I don't have any opengl errors, or compiler errors, so I have no idea what I'm doing wrong.我没有任何 opengl 错误或编译器错误,所以我不知道我做错了什么。 I'm using cmake and ninja to build, if that's any help.我正在使用 cmake 和 ninja 来构建,如果有帮助的话。 The cmake file and the build.sh file I am using to build will be included below. cmake 文件和我用来构建的 build.sh 文件将包含在下面。

CMakeLists: CMakeLists:

cmake_minimum_required (VERSION 3.5)

if(POLICY CMP0072)
    cmake_policy(SET CMP0072 NEW)
else(NOT POLICY CMP0072)
    message(STATUS "Error")
endif(POLICY CMP0072)

project (opengl-learning)

find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS} ${OPENGL_INCLUDE_DIR})

set (GCC_COVERAGE_LINK_FLAGS "-lglfw3 -pthread -ldl -lGLU -lGL -lrt -lXrandr -lXxf86vm -lXi -lXinerama -lX11 -lGLEW -lGLU -lGL")

add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++14")
set (source_dir "${PROJECT_SOURCE_DIR}/src/")

file (GLOB source_files "${source_dir}/*.cpp")

add_executable (opengl-learning ${source_files})

find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)

target_link_libraries(opengl-learning ${GLFW_LIBRARIES} ${GLEW_LIBRARIES} ${OPENGL_LIBRARIES})

build.sh:构建.sh:

#!/bin/sh

cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Debug
ninja

I can put all of my code on github and send a link to the repo if necessary.我可以将我的所有代码放在 github 上,并在必要时发送指向 repo 的链接。

The file which contains the implementation of the stb methods ( src/vendor/stb_image/stb_image.cpp ) is not part of your compilation as can be seen by checking which cpp files are mentioned in your build command.包含 stb 方法实现的文件 ( src/vendor/stb_image/stb_image.cpp ) 不是编译的一部分,这可以通过检查构建命令中提到的 cpp 文件来看出。

The reason for this is that the cmake file is set up to only compile files in the src folder, but not files in the src/vendor/stb_image/ folder.原因是cmake文件设置为只编译src文件夹中的文件,不编译src/vendor/stb_image/文件夹中的文件。 You have to add those files in order to get the implementation of the library into your project.您必须添加这些文件才能将库的实现放入您的项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM