AND4011002849
AND4011002849

Reputation: 2001

Cannot specify link libraries for target which is not built by this project

I'm following this tutorial to add firebase with C++ in my android project, I'm using CMake version 3.10.2 When I run the project, I get the following error:

Error configuring CMake server
(C:\Users\User\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin).
Check for working C compiler:
C:/Users/User/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
Check for working C compiler:
C:/Users/User/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.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:/Users/User/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe
Check for working CXX compiler:
C:/Users/User/AppData/Local/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe
-- works Detecting CXX compiler ABI info Detecting CXX compiler ABI info - done Detecting CXX compile features Detecting CXX compile
features - done Found PkgConfig:
C:/ProgramData/chocolatey/bin/pkg-config.exe (found version "0.28") 
CMake Error at CMakeLists.txt:52 (target_link_libraries):   Cannot
specify link libraries for target "firebase_analytics;firebase_app"  
which is not built by this project.


Configuring incomplete, errors occurred!

My CMakeLists.txt file:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

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).
        native-lib.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        native-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})
# Add Firebase libraries to the target using the function from the SDK.
add_subdirectory(${FIREBASE_CPP_SDK_DIR} bin/ EXCLUDE_FROM_ALL)


# The core Firebase library (firebase_app) is required to use any Firebase product,
# and it must always be listed last.
set(firebase_libs firebase_analytics firebase_app)
target_link_libraries(${target_name} "${firebase_libs}")

Upvotes: 1

Views: 12584

Answers (2)

Kent Boogaart
Kent Boogaart

Reputation: 178740

This can also be caused by not listing your library first. For example, if you have this:

add_library( my_native_code
             SHARED

             # Other stuff here
             )

target_link_libraries(
    android)

This will fail with:

Cannot specify link libraries for target "android" which is not built by this project.

You need to ensure your library is listed first:

target_link_libraries(
    my_native_code
    android)

Upvotes: 3

Kevin
Kevin

Reputation: 18273

I'm noticing that in this line:

target_link_libraries(${target_name} "${firebase_libs}")

The variable target_name is not defined, at least not in the CMake code you have provided. You will need to substitute the name of your target here, instead of the generic variable name used in the Firebase examples. Try this instead:

target_link_libraries(native-lib "${firebase_libs}")

Upvotes: 2

Related Questions