How to build static library for cross compilation to Linux?

Hello,

I am trying to make a little plugin to add the leveldb library to ue4 for use in game (with the snappy optional dependency). **I have it working on windows after building the static .libs with VS. **

I would really like to support linux and mac platforms so I need to build the .a static libraries. I’ve tried doing this by spinning up an Ubuntu machine, installing clang and compiling it as per the github page and I get the libraries but I am unable to link these on my windows machine (I assume because its not exactly the same compiler).

The errors I get are:

1>  C:/UnrealToolchains/v13_clang-7.0.1-centos7/x86_64-unknown-linux-gnu/bin\x86_64-unknown-linux-gnu-ld: C:/Users/Admin/Documents/Unreal Projects/leveldb/Plugins/LDBPlugin/Source/ThirdParty/leveldb/lib\libleveldb.a(env_posix.cc.o): relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC
1>  C:/UnrealToolchains/v13_clang-7.0.1-centos7/x86_64-unknown-linux-gnu/bin\x86_64-unknown-linux-gnu-ld: C:/Users/Admin/Documents/Unreal Projects/leveldb/Plugins/LDBPlugin/Source/ThirdParty/leveldb/lib\libleveldb.a(comparator.cc.o): relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC
1>  C:/UnrealToolchains/v13_clang-7.0.1-centos7/x86_64-unknown-linux-gnu/bin\x86_64-unknown-linux-gnu-ld:

If I understand correctly this is because ue4 wants to make these modules into DLLs. I have tried to recompile with -fPIC however I am not great with cmake so I may have not done so properly.

Does anyone have any experience getting this type of thing to work? I have seen a few other references to this issue but unfortunately none of these mentions led me any where. It looks like using the toolchain from the commandline is an option but I assume I am not nearly familiar enough with cmake to be able to replicate all the build steps for leveldb.

Any suggestions would be greatly appreciated. Thank you!

Dear valued Unreal Engine user,

From the error, it does look as though you will need to recompile the static library with position independent code.

You should be able to use some of the engine third party libraries as an example.

For example:

Engine/Source/ThirdParty/HarfBuzz/harfbuzz-2.4.0/BuildForUE/Linux/BuildForLinux.sh

In this case you can add -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=true to the call to cmake. If you run make with VERBOSE=1 it should output the command-line it sends to the compiler, which should show it passing -fpic to clang.

Also, another point to note, is that if compiling libraries it is recommend to do so in a container on an older Linux distribution, such as CentOS 7 or 8. This is to maintain compatibility with as many distributions as possible.

There is an example script here, this may have gone stale, so you may have better experience using docker, a chroot or mock:

Engine/Build/BatchFiles/Linux/ContainerBuildThirdParty.sh

Thank you for your continued support,

1 Like