首页 > 解决方案 > clang++:错误:链接器命令失败,退出代码为 1

问题描述

从 CPP 文件访问静态库 libadd.a。

CPP 文件是使用 CMakeList.txt 编译的,该文件编译和应用程序生成 libnative-lib.so 文件。

更新了 CMakeList.txt

**target_link_libraries(
                       native-lib
                       ${log-lib}
                       ${CMAKE_SOURCE_DIR}/libadd.a)**

在 native-lib.cpp 文件中访问函数 add()

#include <jni.h>
#include <string>
#include "../../../staticLib/files/add.hpp"

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_oncall_myapplication_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello SUCESSS its 12";

    std::string hello1 = "Hello FAILED it is not 12";

    addition::my::work::abc     Obj;
    Obj.add(5,7);

    if(Obj.add(5,7) == 12)
    {
     return env->NewStringUTF(hello.c_str());
    }
    else
    {
    return env->NewStringUTF(hello1.c_str());
    }

}

尝试构建代码时出错。

构建命令失败。使用参数执行进程 /home/oncall/Desktop/Android/Sdk/cmake/3.6.4111459/bin/cmake 时出错 {--build /home/oncall/Bha/MyApplication3/app/.externalNativeBuild/cmake/debug/x86_64 - -target native-lib} [1/1] 链接 CXX 共享库 ../../../../build/intermediates/cmake/debug/obj/x86_64/libnative-lib.so FAILED: : && /home /oncall/Desktop/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=x86_64-none-linux-android21 --gcc-toolchain=/home/oncall/Desktop/ Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/oncall/Desktop/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC - g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa, 需要动态 R_X86_64_PC32 对 '_ZN8addition2my4work3abc5countE' 重新定位,这可能在运行时溢出;使用 -fPIC /home/oncall/Desktop/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../ 重新编译。 ./../x86_64-linux-android/bin/ld:警告:共享库文本段不可共享 ../../../../libadd.a(add.o):add.cpp:function addition::my::work::abc::getCount(): error: undefined reference to 'addition: :my::work::abc::count' ../../../../libadd.a(add.o):add.cpp:函数添加::my::work::abc:: ~abc(): 错误: 未定义引用'addition::my::work::abc::count' ../../../../libadd.a(add.o):add.cpp:函数addition::my::work::abc::~abc(): error: undefined reference to 'addition::my::work::abc::count' clang++: error: linker command failed with exit code 1 (使用 -v 查看调用)忍者:构建停止:子命令失败。

libadd.a 的 add.cpp

#include "add.hpp"

namespace addition{
namespace my{
namespace work{

int abc::add(int x, int y) {

    return (x+y);
}


int abc::getCount(){
    return count;
}

abc::~abc(){
    count--;
}
};
};
};

标签: c++android-studiogradlecmakestatic-libraries

解决方案


推荐阅读