Luca96 / dlib-for-android

Compile and embed Dlib in your Android projects with ease.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CMake Error at CMakeLists.txt:77 (add_library): Cannot find source file: src/main/cpp/native-lib.cpp

kleysonr opened this issue · comments

Following all the steps and doing the configuration I am getting the error:

Build command failed.
Error while executing process /data/Android/Sdk/cmake/3.6.4111459/bin/cmake with arguments {-H/data/dev/AndroidStudioProjects/OpenCVDL/app/src/main/cpp -B/data/dev/AndroidStudioProjects/OpenCVDL/app/.externalNativeBuild/cmake/debug/armeabi-v7a -DANDROID_ABI=armeabi-v7a -DANDROID_PLATFORM=android-21 -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/data/dev/AndroidStudioProjects/OpenCVDL/app/build/intermediates/cmake/debug/obj/armeabi-v7a -DCMAKE_BUILD_TYPE=Debug -DANDROID_NDK=/data/Android/Sdk/ndk-bundle -DCMAKE_C_FLAGS=-O3 -DCMAKE_CXX_FLAGS=-std=c++11 -frtti -fexceptions -DCMAKE_TOOLCHAIN_FILE=/data/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DCMAKE_MAKE_PROGRAM=/data/Android/Sdk/cmake/3.6.4111459/bin/ninja -GAndroid Gradle - Ninja -DANDROID_PLATFORM=android-21 -DANDROID_TOOLCHAIN=clang -DANDROID_STL=c++_shared -DANDROID_CPP_FEATURES=rtti exceptions}
-- Check for working C compiler: /data/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
-- Check for working C compiler: /data/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -- 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: /data/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++
-- Check for working CXX compiler: /data/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /data/dev/AndroidStudioProjects/OpenCVDL/app/.externalNativeBuild/cmake/debug/armeabi-v7a

CMake Error at CMakeLists.txt:77 (add_library):
  Cannot find source file:

    src/main/cpp/native-lib.cpp

  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
  .hxx .in .txx


CMake Error: CMake can not determine linker language for target: native-lib

Any idea ?

Best Regards.

Hi @Luca96

I didn't close this issue because I was still having the problem.
I think that now I figured out the problem.

I'm using Android Studio 3.3.2 and all the screens to 'Create Android Project' is different from those ones in your blogpost https://medium.com/@luca_anzalone/setting-up-dlib-and-opencv-for-android-3efdbfcf9e7f

When I select to create a new 'Native C++ ' project, the CMakefileLists.txt file is created inside the app/src/main/cpp folder. But in the blogpost we have:

build.gradle (app):

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

and CMakefileLists.txt :

add_library( # Sets the name of the library.
        ${TARGET_NAME}

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        src/main/cpp/native-lib.cpp )

So, in my case the created CMakefileLists.txt is in the wrong place based on the configuration proposed by you. Then I had to move it from app/src/main/cpp to app folder to fix the issue.

Do you remember if you moved the file manually or if your Android Studio has already created it in the app folder ?

I suggest you to include this detail in the blogpost and also update the setup.sh to check and move the file, if needed, during the setup phase.

Best Regards.

Actually I use AndroidStudio 3.2 and the default location of CMakeLists.txt is app/.

The CMakeLists.txt could be located anywhere, it's not important where it is but its path must be defined in build.gradle to have everything working.

The same is true for the path of the libraries (dlib, opencv). It's necessary that their own path is defined inside build.gradle like so:

sourceSets {
            main {
                jniLibs.srcDirs = [
                        "src/main/cppLibs/dlib/lib",
                        "src/main/cppLibs/opencv"
                ]
            }
        }

For a more complete reference you can examine a project I've made, here.