Intrin_avx.hpp compile error

Hi, guys, can you give me some advice for the problem.

System information (version)
  • OpenCV => 4.2.0
  • Operating System / Platform => ubuntu 16.04 64 Bit
  • Compiler => g++ 7.5.0
Detailed description

Compiling the single intrin_avx.hpp fails when using avx/avx2 supported device.

[ 50%] Building CXX object CMakeFiles/test.dir/test.cpp.o
In file included from /home/workspace/Test/test.cpp:2:0:
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:17:1: error: ‘CV_CPU_OPTIMIZATION_HAL_NAMESPACE_BEGIN’ does not name a type
 CV_CPU_OPTIMIZATION_HAL_NAMESPACE_BEGIN
 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:24:8: error: ‘__m256’ does not name a type
 inline __m256 _v256_combine(const __m128& lo, const __m128& hi)
        ^~~~~~
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:27:8: error: ‘__m256d’ does not name a type
 inline __m256d _v256_combine(const __m128d& lo, const __m128d& hi)
        ^~~~~~~
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:30:35: error: ‘__m256i’ does not name a type
 inline int _v_cvtsi256_si32(const __m256i& a)
                                   ^~~~~~~
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp: In function ‘int cv::_v_cvtsi256_si32(const int&)’:
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:31:28: error: ‘_mm256_castsi256_si128’ was not declared in this scope
 { return _mm_cvtsi128_si32(_mm256_castsi256_si128(a)); }
                            ^~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:31:28: note: suggested alternative: ‘_v_cvtsi256_si32’
 { return _mm_cvtsi128_si32(_mm256_castsi256_si128(a)); }
                            ^~~~~~~~~~~~~~~~~~~~~~
                            _v_cvtsi256_si32
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:31:10: error: ‘_mm_cvtsi128_si32’ was not declared in this scope
 { return _mm_cvtsi128_si32(_mm256_castsi256_si128(a)); }
          ^~~~~~~~~~~~~~~~~
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:31:10: note: suggested alternative: ‘_v_cvtsi256_si32’
 { return _mm_cvtsi128_si32(_mm256_castsi256_si128(a)); }
          ^~~~~~~~~~~~~~~~~
          _v_cvtsi256_si32
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp: At global scope:
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:33:8: error: ‘__m256i’ does not name a type
 inline __m256i _v256_shuffle_odd_64(const __m256i& v)
        ^~~~~~~
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:36:8: error: ‘__m256d’ does not name a type
 inline __m256d _v256_shuffle_odd_64(const __m256d& v)
        ^~~~~~~
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:40:8: error: ‘__m256i’ does not name a type
 inline __m256i _v256_permute2x128(const __m256i& a, const __m256i& b)
        ^~~~~~~
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:44:8: error: ‘__m256’ does not name a type
 inline __m256 _v256_permute2x128(const __m256& a, const __m256& b)
        ^~~~~~
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:48:8: error: ‘__m256d’ does not name a type
 inline __m256d _v256_permute2x128(const __m256d& a, const __m256d& b)
        ^~~~~~~
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp: In function ‘_Tpvec cv::v256_permute2x128(const _Tpvec&, const _Tpvec&)’:
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:53:17: error: ‘_v256_permute2x128’ was not declared in this scope
 { return _Tpvec(_v256_permute2x128<imm>(a.val, b.val)); }
                 ^~~~~~~~~~~~~~~~~~
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:53:17: note: suggested alternative: ‘v256_permute2x128’
 { return _Tpvec(_v256_permute2x128<imm>(a.val, b.val)); }
                 ^~~~~~~~~~~~~~~~~~
                 v256_permute2x128
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp: At global scope:
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:56:8: error: ‘__m256i’ does not name a type
 inline __m256i _v256_permute4x64(const __m256i& a)
        ^~~~~~~
/usr/local/include/opencv4/opencv2/core/hal/intrin_avx.hpp:60:8: error: ‘__m256d’ does not name a type

Steps to reproduce
  • test.cpp
#include <stdio.h>
#include "opencv2/core/hal/intrin_avx.hpp"

int main() {
    printf("test\n");
    return 0; 
}
  • CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.15)
PROJECT(test)

SET(CMAKE_CXX_STANDARD 11)

# avx
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx2")

# opencv
MESSAGE(STATUS "FIND OpenCV on LINUX.")
FIND_PACKAGE(OpenCV REQUIRED)

ADD_EXECUTABLE(test test.cpp)

TARGET_LINK_LIBRARIES(
        test
        ${OpenCV_LIBS}
)

please do not bother with such an outdated (prebuilt ?) version
also, the problem can only be mended by building the opencv libs from src.
if the problem persists, there’s a CPU_DISPATCH flag, where you ca remove avx support

Thanks for your advice. Alexander Alekhin told me that the intrin_avx.hpp could not be directly included. I fix it by including opencv2/core/simd_intrinsics.hpp and adding -maxv2 compile flag.

related: How to fix the compile error when include the opencv intrin_avx.hpp - Stack Overflow