编译包含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文件中。

我现在的设置是,在使用CMake配置的预编译头中包含stb映像头文件,并在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

最新更新