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
  • 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](https://stackoverflow.com/a/5099229/3440745). 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](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html) using `-G` option. – Tsyvarev Mar 11 '21 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. – Some Hardware Guy Mar 12 '21 at 14:56
  • 1
    Printing `%1` instead of real path is actually a problem of Ninja (like [that](https://github.com/ninja-build/ninja/issues/1799)). 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. – Tsyvarev Mar 12 '21 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 – Some Hardware Guy Mar 12 '21 at 15:17
  • maybe the compiler is not being called right. – Some Hardware Guy Mar 12 '21 at 15:18
  • 1
    Have you checked [that question](https://stackoverflow.com/questions/28342812/cmake-error-running-link-command-1-is-not-a-valid-win32-application) about the similar error message? – Tsyvarev Mar 12 '21 at 15:20
  • Wow that was helpful thank you. I do have the compilers set as .cmd but the linker txt file it created did not! When I manually changed it in the txt file it worked. Thank you for helping me. – Some Hardware Guy Mar 12 '21 at 17:14

1 Answers1

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)
  • 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. – Some Hardware Guy Mar 12 '21 at 14:56
  • No worries. Please ask your question in another post so I can better help you though. –  Mar 13 '21 at 03:08