0

I don't know why this is giving me so much trouble. I'm trying to move from compiling this .so library on linux to compiling it on a windows machine. For some reason whenever I run cmake .. cmake always goes off and uses MSVC instead of the compiler I'm telling it to use in my cmake file. Maybe I've been staring at it for too long but I don't get what I have wrong. I even tried using the cmake in the Android sdk, same problem. What did I miss?

My cmake file:

cmake_minimum_required(VERSION 3.10.2)
project(TEST)


set(CMAKE_TOOLCHAIN_FILE "C:/Users/myusername/AppData/Local/Android/sdk/ndk-bundle/build/cmake/android.toolchain.cmake")
set(ANDROID_ABI arm64-v8a)

set(TOOLCHAIN "C:/Users/myusername/AppData/Local/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64")


set(CC ${TOOLCHAIN}/bin/aarch64-linux-android28-clang)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN}/bin/aarch64-linux-android28-clang++)

set(CMAKE_C_COMPILER ${TOOLCHAIN}/bin/aarch64-linux-android28-clang)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN}/bin/aarch64-linux-android28-clang++)

include_directories(${CMAKE_SOURCE_DIR}/headers )


add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             #main.cpp
             Image_Reader.cpp
             CV_Main.cpp
             Native_Camera.cpp
             unityInterface.cpp
        )


#tensorflow lite
add_library(lib_tflite SHARED IMPORTED)
set_target_properties(lib_tflite
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/arm64-v8a/libtensorflowlite.so)

#OpenCV
add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/arm64-v8a/libopencv_java4.so)






find_library( log-lib log C:/Users/myusername/AppData/Local/Android/sdk/ndk-bundle/platforms/android-28/arch-arm/usr/lib NO_DEFUALT_PATH)
find_library( camera-lib camera2ndk C:/Users/myusername/AppData/Local/Android/sdk/ndk-bundle/platforms/android-28/arch-arm/usr/lib NO_DEFUALT_PATH)
find_library( media-lib mediandk C:/Users/myusername/AppData/Local/Android/sdk/ndk-bundle/platforms/android-28/arch-arm/usr/lib NO_DEFUALT_PATH)
find_library( android-lib android C:/Users/myusername/AppData/Local/Android/sdk/ndk-bundle/platforms/android-28/arch-arm/usr/lib NO_DEFUALT_PATH)

target_link_libraries(native-lib ${log-lib} ${camera-lib} ${media-lib} ${android-lib} lib_tflite lib_opencv )

My output:

-- Building for: Visual Studio 15 2017
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 6.1.7601.
-- The C compiler identification is MSVC 19.16.27035.0
-- The CXX compiler identification is MSVC 19.16.27035.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/
2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/
2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studi
o/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studi
o/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
7
  • 2
    1. The toolchain file (CMAKE_TOOLCHAIN_FILE variable) should be set before project() call. It is better to specify the toolchain file in the command line, like in that answer. 2. If you do not want to use Visual Studio (the line Building for: Visual Studio 15 2017), then you need to pass proper generator using -G option. Commented Mar 11, 2021 at 23:51
  • @Tsyvarev Thank you that was helpful. Now I get the output: ninja: fatal: CreateProcess: %1 is not a valid Win32 application.cpp.o So far I have not found a way around it. Commented Mar 12, 2021 at 14:56
  • 1
    Printing %1 instead of real path is actually a problem of Ninja (like that). But Ninja tells you that you want to execute a file which cannot be executed on Windows. On Windows executable files are usually have extension .exe, but your compiler (aarch64-linux-android28-clang) doesn't have this extension. Are you sure the compiler is intended to be run on Windows? Probably, you want to use MSYS or MinGW instead. If yes, then choose appropriate CMake generator. Commented Mar 12, 2021 at 15:06
  • Hmm okay. I get a similar error with MinGW Makefiles: [100%] Linking CXX shared library libnative-lib.so Error running link command: %1 is not a valid Win32 application make[2]: *** [CMakeFiles\native-lib.dir\build.make:134: libnative-lib.so] Error 2 make[1]: *** [CMakeFiles\Makefile2:75: CMakeFiles/native-lib.dir/all] Error 2 make: *** [Makefile:83: all] Error 2 Commented Mar 12, 2021 at 15:17
  • 1
    Have you checked that question about the similar error message? Commented Mar 12, 2021 at 15:20

1 Answer 1

1

The 'project' call establishes the toolchain you are going to be using to compile your code.

cmake_minimum_required(VERSION 3.10.2)

# Must establish toolchain code before first project call!
set(CMAKE_TOOLCHAIN_FILE "C:/Users/myusername/AppData/Local/Android/sdk/ndk-bundle/build/cmake/android.toolchain.cmake")
set(ANDROID_ABI arm64-v8a)
set(TOOLCHAIN "C:/Users/myusername/AppData/Local/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64")
set(CC ${TOOLCHAIN}/bin/aarch64-linux-android28-clang)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN}/bin/aarch64-linux-android28-clang++)
set(CMAKE_C_COMPILER ${TOOLCHAIN}/bin/aarch64-linux-android28-clang)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN}/bin/aarch64-linux-android28-clang++)

project(TEST)
Sign up to request clarification or add additional context in comments.

2 Comments

thank you this is helpful. Now I get the output: ninja: fatal: CreateProcess: %1 is not a valid Win32 application.cpp.o So far I have not found a way around it.
No worries. Please ask your question in another post so I can better help you though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.