Compiling libraries in Linux

You can use these switches when building your library (for simplicity assuming that UE4_THIRD_PARTY_DIR holds the absolute path to Engine/Source/ThirdParty directory):

Compile:

-nostdinc++ -I $UE_THIRD_PARTY_DIR/Linux/LibCxx/include/ -I $UE_THIRD_PARTY_DIR/Linux/LibCxx/include/c++/v1

Link:

-nodefaultlibs -L $UE_THIRD_PARTY_DIR/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu/  $UE_THIRD_PARTY_DIR/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu/libc++.a $UE_THIRD_PARTY_DIR/Linux/LibCxx/lib/Linux/x86_64-unknown-linux-gnu/libc++abi.a -lm -lc -lgcc_s -lgcc

In general, if you are not passing STL types between your code and any Unreal module, you may be fine using libstdc++, although you’d need to double check that you won’t run into either binary compatibility (if you don’t redistribute it) or licensing (if you do redistribute it) problems. This depends on your target audience and target Linux distributions.

2 Likes

Another quick question: do I need -std:c++14 as well? I am using Unreal 4.15.3.

clang does not ignore that option when used from UBT, so there may be more factors at play. You can always get the full invocation of clang from both UBT and your library’s build system and compare if you want to drill deeper.

We switched to -std=c++14 in 4.15, so you may also use it. But, it is not as important unless you are passing C++ classes between different dynamic libraries (I recommend against this practice - for the sake of binary compatibility your library’s API should be extern “C” {…}).

We have a plugin which uses 3rd party library. I gather that we need to compile this 3rd party library using Clang and libc++. To understand how to use Unreal’s built-in version of libc++, I was looking in to BuildThirdPaty.sh to see how Unreal does that. However I can’t find anywhere in this script statements that sets up clang and libc++. It seems that this script just invokes individual make for some 3rd party libraries.

So my question is, how do I invokes Unreal build system to compile my plugin’s third party libraries and make sure same Clang and libc++ versions are used as rest of the Unreal? It would be also useful if someone can tell us which version of libc++ is used by specific Unreal versions and what compiler flags should we pass on.

I tried this out and surprising its not working (I’m surprised because this is exactly the options LinuxToolChain.cs in UnrealBuildTool specifies). While my libraries gets compiled successfully, the linking to executable end up with tons of error messages such as undefined reference to std::__1::…`. One things I suspect is that clang is ignoring -nostdinc++ for executables and falls back to GCC headers because I believe std::__1 indicates linking must be done to libstdc++ and not libc++.

Update: if I replace -nodefaultlibs with -stdlib=libc++ then I don’t get these compile errors.

1 Like

I’m adding this comment so that if someone else searches for this thread they can save a bit of effort.
I was able to successfully link google breakpad libraries with this, at least the client libs, although I could never link the executables. I spent a lot of time looking at failed make results before realizing that it had already compiled the libs I needed (I used the -stdlib=libc++ suggested below).

1 Like

I’m going through this right now, and wanted to comment and mention that -nodefaultlibs and -nostdinc++ are actually gcc flags, not clang flags. That said, I have successfully used gcc along with these flags to build my third party library with the UE4-included libc++ and integrate it with Unreal, so clang is not necessarily a requirement.

yeap, this topic helps a lot with final building grpc framework (1.59) on Linux (Ubuntu 22.04) and integrating in UE (5.2).

with -nodefaultlibs there is a problem with building.

final command line:

CXXFLAGS="-fPIC -std=c++17 -nostdinc++ -I/home/igor/Documents/Repos/UnrealEngine/UnrealEngine/Engine/Source/ThirdParty/Unix/LibCxx/include -I/home/igor/Documents/Repos/UnrealEngine/UnrealEngine/Engine/Source/ThirdParty/Unix/LibCxx/include/c++/v1 -stdlib=libc++ -L/home/igor/Documents/Repos/UnrealEngine/UnrealEngine/Engine/Source/ThirdParty/Unix/LibCxx/lib/Unix/x86_64-unknown-linux-gnu/  -L/home/igor/Documents/Repos/UnrealEngine/UnrealEngine/Engine/Source/ThirdParty/Unix/LibCxx/lib/Unix/x86_64-unknown-linux-gnu/libc++.a -L/home/igor/Documents/Repos/UnrealEngine/UnrealEngine/Engine/Source/ThirdParty/Unix/LibCxx/lib/Unix/x86_64-unknown-linux-gnu/libc++abi.a -lm -lc -lgcc_s -lgcc
" cmake ../../Repos/grpc -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/home/igor/Documents/Build/grpc/install -DCMAKE_C_COMPILER=/home/igor/Documents/Repos/UnrealEngine/UnrealEngine/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v21_clang-15.0.1-centos7/x86_64-unknown-linux-gnu/bin/clang -DCMAKE_CXX_COMPILER=/home/igor/Documents/Repos/UnrealEngine/UnrealEngine/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v21_clang-15.0.1-centos7/x86_64-unknown-linux-gnu/bin/clang++ -DgRPC_SSL_PROVIDER=package -DOPENSSL_ROOT_DIR=/home/igor/Documents/Repos/openssl_1_1_1n

be ready to edit some grpc headers macro, wrap protobuf headers to undef UE macros, recompile abseil with different function calls (absent in UE environment), download make ssl with the same version as in UE and rebuild grpc with this.

also this article clarifies UE Linux building process .

2 Likes

Hello! Thank you for your suggestion. I packaged Linux with the GRPC library in UE 5.1.1, but I found that the SSL library in UE5 seems to conflict with the SSL library in GRPC. May I ask if the GRPC library compiled using your method will have this issue? Thank you again!

as mentioned in the command above you need to place this lines when cmaking grpc to compile with custom ssl version. before that, check ssl version in the Engine Third Party, clone this ssl version from public openssl repo and make it.
then modify -DOPENSSL_ROOT_DIR= to your local maked version from openssl.

when you cmake grpc (before --build .) - make sure that in log cmake found right ssl version from your maked -DOPENSSL_ROOT_DIR=

if all is ok, in your build.cs with cmaked grpc libs add:
AddEngineThirdPartyPrivateStaticDependencies(Target, "OpenSSL");
don’t add any additional ssl lib with this line.