编译包含 stb_image.h 的源文件时出错

Error when compiling source file which includes stb_image.h

我在编译包含 stb_image.h.

的 C++ 源文件时遇到此特定错误
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

我直接有的时候没有报这个错

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

在我的 main.cpp 文件中。

我现在的设置是,我将 stb 图像头文件包含在使用 CMake 配置的预编译头文件中,并将该预编译头文件包含在我的 main.cpp 文件中。这会是问题所在吗?

main.cpp 文件,

#include "./epch.hpp"

/*
Code that uses STB Image
*/

cmake 文件,

add_executable(App ${app_src})

target_precompile_headers(App PRIVATE src/epch.hpp)

epch.hpp 文件,

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

stbi_impl.cpp 文件,

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

您的 SIMD 指令生成编译器配置似乎有问题。您应该首先禁用 SIMD:

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

如果程序运行正常,您可以尝试调查 SSE2 支持并添加编译器选项 -msse2