opencv / opencv_contrib

Repository for OpenCV's extra modules
Apache License 2.0
9.42k stars 5.76k forks source link

aarch64-linux 交叉 freetype模块失败 #3553

Open geekmengran opened 1 year ago

geekmengran commented 1 year ago

image 当我修改了freetype下的CMakeLists.list文件发现其能找到版本了,但是交叉编译链接时还是报错了。

opt/tool_chain/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/8.3.0/../../../../aarch64-linux-gnu/bin/ld: cannot find -lfreetype
/opt/tool_chain/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/8.3.0/../../../../aarch64-linux-gnu/bin/ld: cannot find -lharfbuzz
collect2: error: ld returned 1 exit status
modules/freetype/CMakeFiles/opencv_freetype.dir/build.make:96: recipe for target 'lib/libopencv_freetype.so' failed
make[2]: *** [lib/libopencv_freetype.so] Error 1
CMakeFiles/Makefile2:3993: recipe for target 'modules/freetype/CMakeFiles/opencv_freetype.dir/all' failed
make[1]: *** [modules/freetype/CMakeFiles/opencv_freetype.dir/all] Error 2
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2

这是我修改的文件内容

set(the_description "FreeType module. It enables to draw strings with outlines and mono-bitmaps/gray-bitmaps.")
if(APPLE_FRAMEWORK)
  ocv_module_disable(freetype)
endif()

if(PKG_CONFIG_FOUND)
  find_package(Freetype REQUIRED)
  find_package(harfbuzz CONFIG REQUIRED)
  pkg_search_module(FREETYPE freetype2)
  pkg_search_module(HARFBUZZ harfbuzz)
  ocv_check_modules(FREETYPE freetype2)
  ocv_check_modules(HARFBUZZ harfbuzz)
endif()

if(OPENCV_INITIAL_PASS)
  if(NOT FREETYPE_FOUND)
    message(STATUS "freetype2:   NO")
  else()
    message(STATUS "freetype2:   YES (ver ${FREETYPE_VERSION})")
  endif()

  if(NOT HARFBUZZ_FOUND)
    message(STATUS "harfbuzz:    NO")
  else()
    message(STATUS "harfbuzz:    YES (ver ${HARFBUZZ_VERSION})")
  endif()
endif()

if( FREETYPE_FOUND AND HARFBUZZ_FOUND )
  ocv_define_module(freetype opencv_core opencv_imgproc WRAP python java)  
  ocv_target_link_libraries(${the_module} ${FREETYPE_LIBRARIES} ${HARFBUZZ_LIBRARIES})
  ocv_include_directories( ${FREETYPE_INCLUDE_DIRS} ${HARFBUZZ_INCLUDE_DIRS} )
else()
  ocv_module_disable(freetype)
endif()
Kumataro commented 1 year ago

Hello.

  1. Debugging locally modified code is not possible.
  2. OpenCV 3.4.5 is too old. Please use the 4.x series. (3.4 will not release officially in the future.).
  3. When you clicked the New Issue button, there should have been a list of what to include. Please could you fill information about the environment of this issue ?

For example) what is OS ued?: Fedora ? CentOS ? Ubuntu ? Debian ? Alpine ? ... And OS Version is ? ....

By analogy from the fragmentary information so far, I think that the cmake sets both FREETYPE_LIBRARIES(-lfreetype) and HARFBUZZ_LIBRARIES(-lharfbuzz) , but linker cannot find out these libraries.

It seems that you should check your ld settings or re-install these libraries.

It is hard to more advice/comment without additional information.

Kumataro commented 1 year ago

I expect that you tried cross compile on x86_64 host for aarch64 target.

gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu

In this case, you should set correct CMAKE_SYSROOT, PKG_CONFIG_PATH, and ...,

geekmengran commented 1 year ago

Hello.

  1. Debugging locally modified code is not possible.
  2. OpenCV 3.4.5 is too old. Please use the 4.x series. (3.4 will not release officially in the future.).
  3. When you clicked the New Issue button, there should have been a list of what to include. Please could you fill information about the environment of this issue ?

For example) what is OS ued?: Fedora ? CentOS ? Ubuntu ? Debian ? Alpine ? ... And OS Version is ? ....

By analogy from the fragmentary information so far, I think that the cmake sets both FREETYPE_LIBRARIES(-lfreetype) and HARFBUZZ_LIBRARIES(-lharfbuzz) , but linker cannot find out these libraries.

It seems that you should check your ld settings or re-install these libraries.

It is hard to more advice/comment without additional information.

ubuntu18.04,是否需要交叉编译freetype和HARFBUZZ这两个库

Kumataro commented 1 year ago

ubuntu18.04,是否需要交叉编译freetype和HARFBUZZ这两个库

Because you didn't answer about not all of your environment, I don't know I think that's how it is normally.

However, this is question that how to cross compile, NOT an OpenCV bug. So please could you ask on the Q&A forum ?

long-fu commented 1 year ago

我也遇到了这个问题

make sh

  ./configure \
  CC=aarch64-linux-gnu-gcc \
  --host=arm-linux \
  --prefix=${pwd}/_install \
  --enable-shared \
  --enable-static \
  --with-zlib=no \
  --enable-freetype-config \
  --with-png=n

  make -j$(nproc) && make install

/usr/lib/gcc-cross/aarch64-linux-gnu/9/../../../../aarch64-linux-gnu/bin/ld: cannot find -lharfbuzz
collect2: error: ld returned 1 exit status
make: *** [config.mk:55:/home/haoshuai/code/develop_doc/jx/source/freetype-2.13.2/objs/libfreetype.la] 错误 1

environment

> aarch64-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=aarch64-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc-cross/aarch64-linux-gnu/9/lto-wrapper
Target: aarch64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.4.0-1ubuntu1~20.04.2' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libquadmath --disable-libquadmath-support --enable-plugin --enable-default-pie --with-system-zlib --without-target-system-zlib --enable-libpth-m2 --enable-multiarch --enable-fix-cortex-a53-843419 --disable-werror --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=aarch64-linux-gnu --program-prefix=aarch64-linux-gnu- --includedir=/usr/aarch64-linux-gnu/include
Thread model: posix
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.2) 

> uname -a
Linux xxxxx #85~20.04.1-Ubuntu SMP Mon Jul 17 09:42:39 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Kumataro commented 1 year ago

Hello.

  1. Please could you use English ? Translation is needed.
  2. For cross compiling, OpenCV propose to use -DCMAKE_TOOLCHAIN_FILE option. It is better to custom to suitable your environment. https://docs.opencv.org/4.x/d0/d76/tutorial_arm_crosscompile_with_cmake.html
  3. There are no configure script in https://github.com/opencv/opencv. So I cannot reproduce your proble,
Kumataro commented 1 year ago

This is appendix. I tried at Ubuntu23.04 on x86-64 for arm64 with direct-setting, It seems be ok. However, this is not recommended.

kmtr@kmtr-VMware-Virtual-Platform:~/work/opencv_contrib$ git diff -c
diff --git a/modules/freetype/CMakeLists.txt b/modules/freetype/CMakeLists.txt
index 6dd4aaf9..c4ed8233 100644
--- a/modules/freetype/CMakeLists.txt
+++ b/modules/freetype/CMakeLists.txt
@@ -3,8 +3,8 @@ if(APPLE_FRAMEWORK)
   ocv_module_disable(freetype)
 endif()

-ocv_check_modules(FREETYPE freetype2)
-ocv_check_modules(HARFBUZZ harfbuzz)
+# ocv_check_modules(FREETYPE freetype2)
+# ocv_check_modules(HARFBUZZ harfbuzz)

 if(OPENCV_INITIAL_PASS)
   if(NOT FREETYPE_FOUND)
cmake -S opencv -B build4-arm64_3 -GNinja \
    -DCMAKE_TOOLCHAIN_FILE=/home/kmtr/work/opencv/platforms/linux/aarch64-gnu.toolchain.cmake \
    -DOPENCV_EXTRA_MODULES_PATH=opencv_contrib/modules \
    -DFREETYPE_FOUND=ON \
    -DFREETYPE_LIBRARIES=/lib/aarch64-linux-gnu/libfreetype.so \
    -DFREETYPE_INCLUDE_DIRS=/usr/include/freetype2/ \
    -DHARFBUZZ_FOUND=ON \
    -DHARFBUZZ_LIBRARIES=/lib/aarch64-linux-gnu/libharfbuzz.so \
    -DHARFBUZZ_INCLUDE_DIRS=/usr/include/harfbuzz/
-- General configuration for OpenCV 4.8.0-dev =====================================
--   Version control:               4.8.0-236-g5e9191558d
--
--   Extra modules:
--     Location (extra):            /home/kmtr/work/opencv_contrib/modules
--     Version control (extra):     4.8.0-17-gc82dea11-dirty
--
--   Platform:
--     Timestamp:                   2023-09-15T11:37:11Z
--     Host:                        Linux 6.2.0-32-generic x86_64
--     Target:                      Linux 1 aarch64
--     CMake:                       3.25.1
--     CMake generator:             Ninja
--     CMake build tool:            /usr/bin/ninja
--     Configuration:               Release
kmtr@kmtr-VMware-Virtual-Platform:~/work/build4-arm64_3$ LANG=C file lib/libopencv_freetype.so.4.8.0
lib/libopencv_freetype.so.4.8.0: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, BuildID[sha1]=XXXX, not stripped
Kumataro commented 1 year ago

BTW, Ubuntu supports multiarch.

In this case, you should set correct CMAKE_SYSROOT, PKG_CONFIG_PATH, and ...,

If you can set correct environment parameters, cmake with pkg-config works well. I try ubuntu22.04 on x86-64 with arm64 packages (no modify CMakefile ). There are small warnings, but it is almost ok.

So OpenCV 4.8.0 can be cross-compiled even if freetype wrapper is enabled . If any problems, there are your environment specific problems, it is not OpenCV bug. So I think this issue can/should be closed.

PKG_CONFIG_PATH=/lib/aarch64-linux-gnu/pkgconfig \
PKG_CONFIG_LIBDIR=/usr/lib/aarch64-linux-gnu \
PKG_CONFIG_SYSROOT_DIR=/ \
cmake -S opencv -B build4-arm64_4 -GNinja \
  -DCMAKE_TOOLCHAIN_FILE=/home/kmtr/work/opencv/platforms/linux/aarch64-gnu.toolchain.cmake \
  -DOPENCV_EXTRA_MODULES_PATH=opencv_contrib/modules
long-fu commented 1 year ago

Thank you

Kumataro commented 11 months ago

I think this issue can be closed with https://github.com/opencv/opencv/pull/24629 in now.