make stuck on "[ 50%] Linking CXX executable someExecutable"

Hi, I am trying to compile a simple C++ program and create a library that will later be used in Android Studio. (Compiling the .cpp file via command line works so there is no problem with the code)

I am running on a MacBook Pro Darwin-x86-64-i386 looking to compile for Android arm.

I am getting these errors

ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_gapi.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_stitching.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_alphamat.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_aruco.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_bgsegm.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_bioinspired.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_ccalib.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_dnn_objdetect.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_dnn_superres.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_dpm.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_face.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_freetype.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_fuzzy.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_hfs.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_img_hash.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_intensity_transform.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_line_descriptor.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_mcc.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_quality.4.5.2.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_rapid.4.5.2.dylib: unknown file type
ld: error: too many errors emitted, stopping now (use -error-limit=0 to see all errors)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [cpp_exec] Error 1
make[1]: *** [CMakeFiles/cpp_exec.dir/all] Error 2
make: *** [all] Error 2

These are my script and CMakeLists.txt files:

Script:

#!/bin/bash

SDK=/Users/David/Library/Android/sdk
CMAKE=$SDK/cmake/3.18.1/bin/cmake
NDK=$SDK/ndk/23.0.7272597
TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake
TOOLCHAIN_BINARIES=$NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin
LD=$TOOLCHAIN_BINARIES/ld
ABI=arm64-v8a
MIN_SDK=16
STL=c++_shared
STD=-std=c++11 -o 
CXX=$TOOLCHAIN_BINARIES/aarch64-linux-android30-clang++
CMAKE_ARGS=""

export CXX=$CXX

$CMAKE -DCMAKE_CXX_FLAGS=$STD -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN_FILE -DANDROID_ABI=$ABI -DANDROID_NATIVE_API_LEVEL=$MIN_SDK -DANDROID_STL=$STL $CMAKE_ARGS

CMakeLists.txt:

cmake_minimum_required(VERSION 3.7 FATAL_ERROR)

project(shared_library_cpp VERSION 1.0.0 LANGUAGES CXX)
set(OpenCV_DIR "/usr/local/Cellar/opencv/4.5.2_1/lib/cmake/opencv4")
find_package(OpenCV REQUIRED)
set(CMAKE_CXX_STANDARD 11)
set(CMALE_CXX_STANDARD_REQUIRED ON)
include_directories(${OpenCV_INCLUDE_DIRS})

add_library(shared_library_cpp SHARED lib_cpp.cpp cpp.def)
add_executable(cpp_exec lib_cpp.cpp)
target_link_libraries( shared_library_cpp ${OpenCV_LIBS} )
target_link_libraries( cpp_exec ${OpenCV_LIBS} )


set_target_properties(shared_library_cpp PROPERTIES
    PUBLIC_HEADER lib_cpp.h
    VERSION ${PROJECT_VERSION}
    SOVERSION 1
    OUTPUT_NAME "cpp"
    XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "MacOS_ID"
)

The openCV Path is correct so and the command 'file someOpenCVDynamicLibrary.dylib" gives me “Mach-O 64-bit dynamically linked shared library x86_64” if it helps in anyway.

Your OpenCV is compiled for your host; you’ll need to compile OpenCV yourself using the same toolchain setup as your project is using.

1 Like