首页 > 解决方案 > 如何解决“ld:错误:.* :relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145225728 is not in [-4294967296, 4294967295]”

问题描述

我正在使用 cmake 和 ninja 构建一个 Android 可执行文件:

cmake -H../ -DCMAKE_CXX_FLAGS=-std=c++11 -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake -DANDROID_ABI=$ABI -DANDROID_NDK=$NDK -DANDROID_PLATFORM=android-$MIN_SDK_VERSION -DCMAKE_ANDROID_ARCH_ABI=$ABI -DCMAKE_ANDROID_NDK=$NDK -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=$ABI -DCMAKE_MAKE_PROGRAM=$SDK_PATH/cmake/$CMAKE_VERSION/bin/ninja.exe -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=$MIN_SDK_VERSION -DANDROID_STL=c++_static -B$BUILD_DIR -GNinja

ninja

该项目在文件的全局空间中声明了一些非常大的变量:

BCCH_BCH_Message bcch_bch_Message;
BCCH_DL_SCH_Message bcch_dl_sch_Message;
DL_CCCH_Message dl_ccch_Message;
DL_DCCH_Message dl_dcch_Message;
PCCH_Message pcch_Message;
...

当我运行项目时,我将开始收到如下构建错误:

-- The C compiler identification is Clang 11.0.5
-- The CXX compiler identification is Clang 11.0.5
-- Check for working C compiler: D:/Sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
-- Check for working C compiler: D:/Sdk/ndk/22.0.7026061/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: D:/Sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe
-- Check for working CXX compiler: D:/Sdk/ndk/22.0.7026061/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
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Development/CPP/ASNWrapper/build
[4/4] Linking CXX executable asn_wrap
FAILED: asn_wrap
cmd.exe /C "cd . && D:\Sdk\ndk\22.0.7026061\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android29 --gcc-toolchain=D:/Sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=D:/Sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/windows-x86_64/sysroot  -fPIC -O2 -DNDEBUG  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id=sha1 -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,--gc-sections CMakeFiles/asn_wrap.dir/src/test_dl_dcch.cpp.o CMakeFiles/asn_wrap.dir/src/nas_asn_wrap.cpp.o CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o  -o asn_wrap -LD:/Development/CPP/ASNWrapper/lib/arm64-v8a -lasn1per -lasn1rt -lasn1rt3gpp -losys3gpp ../lib/arm64-v8a/libasn_rrc.a -llog -latomic -lm && cd ."
ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0xDF0): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 10986287104 is not in [-4294967296, 4294967295]; references pcch_Message
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0xE90): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 10986291200 is not in [-4294967296, 4294967295]; references ul_ccch_Message
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0xF30): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 10986291200 is not in [-4294967296, 4294967295]; references ul_ccch1_Message
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0xFD0): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 10986287104 is not in [-4294967296, 4294967295]; references ul_dcch_Message
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1070): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 14935199744 is not in [-4294967296, 4294967295]; references 
rrcReconfig
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1110): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145197056 is not in [-4294967296, 4294967295]; references 
rrcReconfigComp
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x11B0): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145225728 is not in [-4294967296, 4294967295]; references 
sib1
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1250): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145434624 is not in [-4294967296, 4294967295]; references 
sysInfo
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x12F0): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145381376 is not in [-4294967296, 4294967295]; references 
sib2
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1390): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145381376 is not in [-4294967296, 4294967295]; references 
sib3
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1430): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145385472 is not in [-4294967296, 4294967295]; references 
sib4
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x14D0): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145426432 is not in [-4294967296, 4294967295]; references 
sib5
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1570): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145434624 is not in [-4294967296, 4294967295]; references 
sib6
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1610): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145434624 is not in [-4294967296, 4294967295]; references 
sib7
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x16B0): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145434624 is not in [-4294967296, 4294967295]; references 
sib8
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1750): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145434624 is not in [-4294967296, 4294967295]; references 
sib9
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: /buildbot/src/android/ndk-release-r22\toolchain/llvm-project/libcxx/../../../toolchain/llvm-project/libcxxabi/../libcxx/include/__threading_support:367:(.text.__cxa_guard_abort+0x10): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20144017408 is not in [-4294967296, 4294967295]
ld: error: /buildbot/src/android/ndk-release-r22\toolchain/llvm-project/libcxx/../../../toolchain/llvm-project/libcxxabi/../libcxx/include/__threading_support:367:(.text._ZN12_GLOBAL__N_115fallback_mallocEm+0x10): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20143935488 is not in [-4294967296, 4294967295]
ld: error: /buildbot/src/android/ndk-release-r22\toolchain/llvm-project/libcxx/../../../toolchain/llvm-project/libcxxabi/../libcxx/include/__threading_support:367:(.text.__cxa_guard_acquire+0x2C): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20144021504 is not in [-4294967296, 4294967295]
ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x17F0): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 11474681856 is not in [-4294967296, 4294967295]; references 
cellGroupConfig
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: too many errors emitted, stopping now (use -error-limit=0 to see all errors)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

如果我评论看起来非常大的全局变量,我不会收到错误消息:

// DL_CCCH_Message dl_ccch_Message;
// DL_DCCH_Message dl_dcch_Message;

但我确实需要这些变量。在过去的三天里,我在互联网上搜索了很多。有人说要添加“-fPIC”,我做了,但这些错误仍然没有解决。

这是头文件中一个这样的变量的声明:

/**************************************************************/
/*                                                            */
/*  DL_CCCH_Message                                           */
/*                                                            */
/**************************************************************/
/*
DL-CCCH-Message ::= SEQUENCE {
   message [0] EXPLICIT DL-CCCH-MessageType
}
*/
typedef struct EXTERN DL_CCCH_Message {
   DL_CCCH_MessageType message;
} DL_CCCH_Message;

这个的定义在 .so 文件中,代码没有给我。所以,我可以照原样使用它。

请提出解决这些错误的建议。

标签: androidc++candroid-ndkclang

解决方案


推荐阅读