wsl2 上编译opencv 4.7.0 with cuda

cd opencv-4.7.0 && mkdir build

cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_CUDA=ON -D WITH_CUBLAS=ON -D WITH_CUDNN=ON -D WITH_LIBV4L=ON -D ENABLE_FAST_MATH=ON -D CUDA_FAST_MATH=ON -D OPENCV_DNN_CUDA=ON -D ENABLE_NEON=OFF -D WITH_OPENMP=ON -D WITH_OPENGL=OFF -D WITH_FFMPEG=ON -D WITH_TBB=ON -D BUILD_TBB=ON -D BUILD_TESTS=OFF -D BUILD_opencv_python3=ON -D BUILD_opencv_python2=OFF -D BUILD_opencv_java=OFF -D WITH_GTK=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF -D OPENCV_ENABLE_NONFREE=ON -DBUILD_OPENJPEG:BOOL=ON -D OPENCV_EXTRA_MODULES_PATH=/mnt/j/opencv_contrib-4.7.0/modules ..

make -j8 && sudo make install

wsl2 编译 opencv 4.7.0 的一个OpenJpeg的问题

这个问题我在wsl2 的 ubuntu20.04 中编译 opencv4.7.0 时遇到了,不清楚在别的wsl 和别的ubuntu 版本上有没有这个问题,最终问题也解决了,不知道别的wsl 环境有没有这个问题,先把解决方法记下了。

在编译时会出以下这样的错误

grfmt_jpeg2000_openjpeg.cpp:(.text._ZN2cv6detail20Jpeg2KOpjDecoderBase10readHeaderEv+0xa1): undefined reference to `opj_stream_set_user_data’
/usr/bin/ld: grfmt_jpeg2000_openjpeg.cpp:(.text._ZN2cv6detail20Jpeg2KOpjDecoderBase10readHeaderEv+0xb0): undefined reference to `opj_stream_set_user_data_length’
/usr/bin/ld: grfmt_jpeg2000_openjpeg.cpp:(.text._ZN2cv6detail20Jpeg2KOpjDecoderBase10readHeaderEv+0xbf): undefined reference to `opj_stream_set_read_function’
/usr/bin/ld: grfmt_jpeg2000_openjpeg.cpp:(.text._ZN2cv6detail20Jpeg2KOpjDecoderBase10readHeaderEv+0xce): undefined reference to `opj_stream_set_skip_function’
/usr/bin/ld: grfmt_jpeg2000_openjpeg.cpp:(.text._ZN2cv6detail20Jpeg2KOpjDecoderBase10readHeaderEv+0xdd): undefined reference to `opj_stream_set_seek_function’
/usr/bin/ld: grfmt_jpeg2000_openjpeg.cpp:(.text._ZN2cv6detail20Jpeg2KOpjDecoderBase10readHeaderEv+0xf5): undefined reference to `opj_stream_destroy’
/usr/bin/ld: grfmt_jpeg2000_openjpeg.cpp:(.text._ZN2cv6detail20Jpeg2KOpjDecoderBase10readHeaderEv+0x110): undefined reference to `opj_create_decompress’
/usr/bin/ld: grfmt_jpeg2000_openjpeg.cpp:(.text._ZN2cv6detail20Jpeg2KOpjDecoderBase10readHeaderEv+0x12e): undefined reference to `opj_destroy_codec’
/usr/bin/ld: grfmt_jpeg2000_openjpeg.cpp:(.text._ZN2cv6detail20Jpeg2KOpjDecoderBase10readHeaderEv+0x150): undefined reference to `opj_set_default_decoder_parameters’

从错误提示看,是找不到Jpeg2000 的某个引用的方法,但为什么找不到就不知道了,在网上一翻查找后,没找到说这个问题的,最后在一篇国外的关于wsl2 和 openjpeg  的文章找到了一丝解决方法,试了一下,最后终于解决了。原文的意思是jpeg2000 所使用的OpenJPEG 在wsl2 中已经包含了,但在编译时还是会提示找不到符号。可能是包含的库少了些什么,需要使用opencv 去编译自己的 openJpeg 所以,需要在cmake 中加入

-DBUILD_OPENJPEG:BOOL=ON 参数。

最后加入这个后,问题解决