c++clinuxstb-image

Error when compiling source file which includes stb_image.h


I get this particular error when compiling a C++ source file which includes stb_image.h.

In file included from /home/zeux/Documents/Projects/cube-game/./lib/stb/stb_image.h:723,
                 from /home/zeux/Documents/Projects/cube-game/src/core/stbi_impl.cpp:2:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/include/emmintrin.h: In function ‘stbi_uc* stbi__resample_row_hv_2_simd(stbi_uc*, stbi_uc*, stbi_uc*, int, int)’:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/include/emmintrin.h:1230:10: error: the last argument must be an 8-bit immediate
 1230 |   return (__m128i)__builtin_ia32_pslldqi128 (__A, __N * 8);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/include/emmintrin.h:1224:10: error: the last argument must be an 8-bit immediate
 1224 |   return (__m128i)__builtin_ia32_psrldqi128 (__A, __N * 8);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make[2]: *** [CMakeFiles/App.dir/build.make:205: CMakeFiles/App.dir/src/core/stbi_impl.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:158: CMakeFiles/App.dir/all] Error 2
make: *** [Makefile:136: all] Error 2

I did not get this error when I directly had

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

In my main.cpp file.

My setup right now is, I include the stb image header file in a precompiled header, which is configured using CMake, and I include that precompiled header in my main.cpp file. Would this be the issue?

main.cpp file,

#include "./epch.hpp"

/*
Code that uses STB Image
*/

cmake file,

add_executable(App ${app_src})

target_precompile_headers(App PRIVATE src/epch.hpp)

epch.hpp file,

/*Other includes*/
#include <stb_image.h>
/*Other includes*/

stbi_impl.cpp file,

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

Solution

  • There seems to be a problem with your compiler configuration for SIMD instruction generation. You should first disable SIMD:

    #define STBI_NO_SIMD
    #define STB_IMAGE_IMPLEMENTATION
    #include <stb_image.h>
    

    If the program works correctly, you can try and investigate SSE2 support and add a compiler option -msse2.