問題來源
嘗試在UE5中使用本地編譯的aws-sdk-cpp,并需要在UE的windows編輯器里,交叉編譯linux版本。
編譯aws-sdk-cpp的方法參照了Build the AWS SDK for C++ on Linux/macOS,在UE項目中引入庫的方法,參照了How to Integrate the AWS C++ SDK with Unreal Engine。
在Windows編輯器中編譯正常,打包Windows也正常,但交叉編譯linux版本時出現(xiàn)鏈接錯誤,提示undefined symbol: Aws::Utils::DateTime::DateTime,如下
[2023.09.14-01.59.44:568][655]UATHelper: Packaging (Linux): ld.lld: error: undefined symbol: Aws::Utils::DateTime::DateTime(std::__1::chrono::time_point<std::__1::chrono::system_clock, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000000l> > > const&)
[2023.09.14-01.59.44:568][655]UATHelper: Packaging (Linux): >>> referenced by AWSCredentials.h:36 (D:/Projects/DT/MapEditorUE/ThirdParty/AWS/Include\aws/core/auth\AWSCredentials.h:36)
解決方案
嘗試了各種動態(tài)庫,靜態(tài)庫,均報同樣錯誤。
最終發(fā)現(xiàn),編譯aws-sdk-cpp時,需要使用clang,而非gcc,因為Unreal的交叉編譯使用的就是clang(似乎還是定制過的),一些c++標(biāo)準(zhǔn)的函數(shù)符號似乎存在差異,導(dǎo)致了這個問題。
使用clang編譯可在cmake前用環(huán)境變量指定,并需要設(shè)定-DCMAKE_CXX_FLAGS="-stdlib=libc++",否則找不到libc++庫
# 指定使用clang作為編譯器
export CXX=/path/to/clang++
export CC=/path/to/clang
# cmake時指定-DCMAKE_CXX_FLAGS="-stdlib=libc++"
cmake ../../aws-sdk-cpp -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=/usr/local/ -DCMAKE_INSTALL_PREFIX=/usr/local/ -DBUILD_SHARED_LIBS=ON -DCMAKE_CXX_FLAGS="-stdlib=libc++"
# make
make -j 8
TIP
如果不設(shè)定-DCMAKE_CXX_FLAGS="-stdlib=libc++",cmake可能會報如下錯誤,因為clang默認(rèn)不會取找gcc的庫
CMake Error at /usr/local/share/cmake-3.22/Modules/CMakeTestCXXCompiler.cmake:62 (message):
The C++ compiler
"/usr/bin/clang++"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /home/SENSETIME/sunxia/Develop/Programs/aws-sdk-cpp-build/test/CMakeFiles/CMakeTmp
Run Build Command(s):/bin/make -f Makefile cmTC_fba77/fast && /bin/make -f CMakeFiles/cmTC_fba77.dir/build.make CMakeFiles/cmTC_fba77.dir/build
make[1]: Entering directory '/home/SENSETIME/sunxia/Develop/Programs/aws-sdk-cpp-build/test/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_fba77.dir/testCXXCompiler.cxx.o
/usr/bin/clang++ -MD -MT CMakeFiles/cmTC_fba77.dir/testCXXCompiler.cxx.o -MF CMakeFiles/cmTC_fba77.dir/testCXXCompiler.cxx.o.d -o CMakeFiles/cmTC_fba77.dir/testCXXCompiler.cxx.o -c /home/SENSETIME/sunxia/Develop/Programs/aws-sdk-cpp-build/test/CMakeFiles/CMakeTmp/testCXXCompiler.cxx
Linking CXX executable cmTC_fba77
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/cmTC_fba77.dir/link.txt --verbose=1
/usr/bin/clang++ -rdynamic CMakeFiles/cmTC_fba77.dir/testCXXCompiler.cxx.o -o cmTC_fba77
/usr/bin/ld: cannot find -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [CMakeFiles/cmTC_fba77.dir/build.make:100: cmTC_fba77] Error 1
make[1]: Leaving directory '/home/SENSETIME/sunxia/Develop/Programs/aws-sdk-cpp-build/test/CMakeFiles/CMakeTmp'
make: *** [Makefile:127: cmTC_fba77/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:152 (project)