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.
I put stb_image.h
in src/vendor/stb_image/
, made a cpp file named stb_image.cpp
with
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
in the file, and put the file in the src/vendor/stb_image/
directory. When I try to include #include "vendor/stb_image/stb_image.h"
and use it, I get this error:
: && /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:
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. 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_LocalBuffer was empty as well. I don't have any opengl errors, or compiler errors, so I have no idea what I'm doing wrong. I'm using cmake and ninja to build, if that's any help. The cmake file and the build.sh file I am using to build will be included below.
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:
#!/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.