NinevaStudios / mqtt-utilities-unreal

MqttUtilities is a plugin for Unreal Engine intended to expose MQTT client functionality to blueprints
Eclipse Public License 1.0
157 stars 60 forks source link

Issue with Position Independent Code (-fPIC) during compilation (UE 5.0.2)(Linux) #21

Open TheMesoria opened 2 years ago

TheMesoria commented 2 years ago

OS: Ubuntu 22.04 LTS UE Version: 5.0.2 (Source code build) Plugin Version: 1.1.0 (Downloaded from releases)

When I am attempting to build your solution into my project, I am unfortunately greeted by those errors:

Full error output ``` >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquittopp/libraries/libmosquittopp_static.a(mosquittopp.cpp.o) >>> referenced by mosquittopp.cpp >>> mosquittopp.cpp.o:(mosqpp::mosquittopp::mosquittopp(char const*, bool)) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquittopp/libraries/libmosquittopp_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol vtable for mosqpp::mosquittopp; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquittopp/libraries/libmosquittopp_static.a(mosquittopp.cpp.o) >>> referenced by mosquittopp.cpp >>> mosquittopp.cpp.o:(mosqpp::mosquittopp::~mosquittopp()) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquittopp/libraries/libmosquittopp_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol mosqpp::mosquittopp::~mosquittopp(); recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquittopp/libraries/libmosquittopp_static.a(mosquittopp.cpp.o) >>> referenced by mosquittopp.cpp >>> mosquittopp.cpp.o:(mosqpp::mosquittopp::~mosquittopp()) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquittopp/libraries/libmosquittopp_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol mosquitto_reinitialise; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(mosquitto.c.o) >>> referenced by mosquitto.c >>> mosquitto.c.o:(mosquitto_new) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol mosquitto_destroy; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(mosquitto.c.o) >>> referenced by mosquitto.c >>> mosquitto.c.o:(mosquitto_new) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol mosquitto__destroy; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(mosquitto.c.o) >>> referenced by mosquitto.c >>> mosquitto.c.o:(mosquitto_reinitialise) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol mosquitto__destroy; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(mosquitto.c.o) >>> referenced by mosquitto.c >>> mosquitto.c.o:(mosquitto_destroy) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol _ui_method; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(net_mosq.c.o) >>> referenced by net_mosq.c >>> net_mosq.c.o:(setup_ui_method) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol _ui_method; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(net_mosq.c.o) >>> referenced by net_mosq.c >>> net_mosq.c.o:(setup_ui_method) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol _ui_method; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(net_mosq.c.o) >>> referenced by net_mosq.c >>> net_mosq.c.o:(setup_ui_method) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol _ui_method; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(net_mosq.c.o) >>> referenced by net_mosq.c >>> net_mosq.c.o:(setup_ui_method) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol _ui_method; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(net_mosq.c.o) >>> referenced by net_mosq.c >>> net_mosq.c.o:(setup_ui_method) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol _ui_method; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(net_mosq.c.o) >>> referenced by net_mosq.c >>> net_mosq.c.o:(cleanup_ui_method) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol _ui_method; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(net_mosq.c.o) >>> referenced by net_mosq.c >>> net_mosq.c.o:(cleanup_ui_method) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol _ui_method; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(net_mosq.c.o) >>> referenced by net_mosq.c >>> net_mosq.c.o:(cleanup_ui_method) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol _ui_method; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(net_mosq.c.o) >>> referenced by net_mosq.c >>> net_mosq.c.o:(net__get_ui_method) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol tls_ex_index_mosq; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(net_mosq.c.o) >>> referenced by net_mosq.c >>> net_mosq.c.o:(net__init_tls) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol tls_ex_index_mosq; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(net_mosq.c.o) >>> referenced by net_mosq.c >>> net_mosq.c.o:(net__init_tls) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol tls_ex_index_mosq; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(net_mosq.c.o) >>> referenced by net_mosq.c >>> net_mosq.c.o:(psk_client_callback) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol net__socket_nonblock; recompile with -fPIC >>> defined in Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a(net_mosq.c.o) >>> referenced by net_mosq.c >>> net_mosq.c.o:(net__try_connect_tcp) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a ```

Is it a problem on my end? Does any other Linux user already know it? The same setup works for MS Windows just fine; Project without plugins boots on Linux; it's just this plugin that fails to compile.

While I am not very experienced with the Unreal Engine Build Tool ecosystem, I look forward to knowing what I did wrong (or not).

tustanivsky commented 2 years ago

Hi @TheMesoria , it looks that the mosquitto library which is used by our plugin was compiled with some settings that make it incompatible with UE5 on Linux. You can try to re-compile it by making sure -fPIC option is there (PIC stands for Position Independent Code) and update the corresponding plugin binaries.

tustanivsky commented 2 years ago

Also, you can try to add AdditionalCompilerArguments = "-fPIC"; to your project's Target.cs file and see if it helps

TheMesoria commented 2 years ago

I already did add AdditionalCompilerArguments = "-fPIC"; to the project, as it was one of my first guesses. I will attempt to recompile mosquitopp and other '.a' with -fPIC and update in the next comment.

TheMesoria commented 2 years ago

After some work (mostly reading CMakeLists.txt) If come up with this solution:

mkdir bin && cd bin cmake ../ -DCMAKE_INSTALL_PREFIX=$(pwd)/install/mosquitto/bin/install -DWITH_STATIC_LIBRARIES=ON -DWITH_PIC=ON make && make install

After those, I copied those libraries to replace the current ones, and it appears to work relatively well. Unfortunately, I run into issues with openSSL lib, which I think are mostly connected to the Unreal Engine Build Tool linker:

Full error output ``` [1/21] Link (lld) libUnrealEditor-MqttUtilities.so 0>ld.lld: Error : undefined symbol: SSL_free >>> referenced by mosquitto.c >>> mosquitto.c.o:(mosquitto__destroy) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a >>> referenced by net_mosq.c >>> net_mosq.c.o:(net__socket_close) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a >>> referenced by net_mosq.c >>> net_mosq.c.o:(net__socket_connect_step3) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: SSL_CTX_free >>> referenced by mosquitto.c >>> mosquitto.c.o:(mosquitto__destroy) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: UI_OpenSSL >>> referenced by net_mosq.c >>> net_mosq.c.o:(ui_open) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a >>> referenced by net_mosq.c >>> net_mosq.c.o:(ui_read) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a >>> referenced by net_mosq.c >>> net_mosq.c.o:(ui_write) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a >>> referenced 1 more times 0>ld.lld: Error : undefined symbol: UI_method_get_opener >>> referenced by net_mosq.c >>> net_mosq.c.o:(ui_open) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: UI_method_get_reader >>> referenced by net_mosq.c >>> net_mosq.c.o:(ui_read) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: UI_method_get_writer >>> referenced by net_mosq.c >>> net_mosq.c.o:(ui_write) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: UI_method_get_closer >>> referenced by net_mosq.c >>> net_mosq.c.o:(ui_close) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: UI_create_method >>> referenced by net_mosq.c >>> net_mosq.c.o:(setup_ui_method) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: UI_method_set_opener >>> referenced by net_mosq.c >>> net_mosq.c.o:(setup_ui_method) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: UI_method_set_reader >>> referenced by net_mosq.c >>> net_mosq.c.o:(setup_ui_method) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: UI_method_set_writer >>> referenced by net_mosq.c >>> net_mosq.c.o:(setup_ui_method) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: UI_method_set_closer >>> referenced by net_mosq.c >>> net_mosq.c.o:(setup_ui_method) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: UI_destroy_method >>> referenced by net_mosq.c >>> net_mosq.c.o:(cleanup_ui_method) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: CONF_modules_unload >>> referenced by net_mosq.c >>> net_mosq.c.o:(net__cleanup) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: OPENSSL_init_crypto >>> referenced by net_mosq.c >>> net_mosq.c.o:(net__init_tls) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: ENGINE_load_builtin_engines >>> referenced by net_mosq.c >>> net_mosq.c.o:(net__init_tls) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: CRYPTO_get_ex_new_index >>> referenced by net_mosq.c >>> net_mosq.c.o:(net__init_tls) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: SSL_in_init >>> referenced by net_mosq.c >>> net_mosq.c.o:(net__socket_close) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: SSL_shutdown >>> referenced by net_mosq.c >>> net_mosq.c.o:(net__socket_close) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: SSL_get_ex_data >>> referenced by net_mosq.c >>> net_mosq.c.o:(psk_client_callback) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a >>> referenced by tls_mosq.c >>> tls_mosq.c.o:(mosquitto__server_certificate_verify) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : too many errors emitted, stopping now (use -error-limit=0 to see all errors) 0>clang++: Error : linker command failed with exit code 1 (use -v to see invocation) ```

I am slightly clueless about what could be a reason for such an output, as I have libssl in public and well-defined paths:

/usr/include/openssl/ssl.h /usr/include/openssl/engine.h etc.

libs are at

>>>>> ls /lib/x86_64-linux-gnu/ | grep ssl
libssl3.so
libssl.a
libssl.so
libssl.so.1.1
libssl.so.3
ossl-modules

So everything looks well and dandy to me. I attempted to add

        PublicDependencyModuleNames.AddRange(
            new string[]
            {
                "Core",
                "OpenSSL"
                // ... add other public dependencies that you statically link with here ...
            }
            );

And It appeared to patch up the issue (?) and I was left with:

Full Error Output ``` 0>ld.lld: Error : undefined symbol: SSL_CTX_load_verify_file >>> referenced by net_mosq.c >>> net_mosq.c.o:(net__tls_load_ca) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>ld.lld: Error : undefined symbol: SSL_CTX_load_verify_dir >>> referenced by net_mosq.c >>> net_mosq.c.o:(net__tls_load_ca) in archive Plugins/MqttUtilities/Source/ThirdParty/Linux/libmosquitto/libraries/libmosquitto_static.a 0>clang++: Error : linker command failed with exit code 1 (use -v to see invocation) ```

I am completely clueless about how to approach this issue, as it seems to be part of the OpenSSL.