Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mosquitto] Static linking broken #20604

Closed
2 of 7 tasks
silabs-beta opened this issue Apr 9, 2024 · 19 comments · Fixed by #20605 or #20642
Closed
2 of 7 tasks

[mosquitto] Static linking broken #20604

silabs-beta opened this issue Apr 9, 2024 · 19 comments · Fixed by #20605 or #20642
Labels

Comments

@silabs-beta
Copy link

Description / Steps to reproduce the issue

I try to use the libmosquitto package in my project with static linking. I use -static linker flag and I get the linker flags for the package like this: pkg-config --libs --static libmosquitto. The output of the pkg-config is -lmosquitto.

Expected behavior

The flags provided by pkg-config should work as is.

Actual behavior

The linker fails like this: cannot find -lmosquitto: No such file or directory
It happens because the static library is called libmosquitto.dll.a, see the file list here.
If I link with the -lmosquitto.dll flag, the link succeeds.
I think the libmosquitto.dll.a should be renamed to libmosquitto.a, but I don't know where to look at.
I think libmosquittopp is affected too, but I haven't tried it.

Verification

Windows Version

MINGW64_NT-10.0-19045

MINGW environments affected

  • MINGW64
  • MINGW32
  • UCRT64
  • CLANG64
  • CLANG32
  • CLANGARM64

Are you willing to submit a PR?

No response

@podsvirov
Copy link
Collaborator

@silabs-beta, can you confirm that #20605 fix your problem? (you can rebuild package himself or download prebuild Artifacts from Checks)

@silabs-beta
Copy link
Author

Unfortunately no, I get various linker errors like these

undefined reference to `__imp_mosquitto_lib_init'
undefined reference to `__imp_mosquitto_new'
undefined reference to `__imp_mosquitto_connect_callback_set'
undefined reference to `__imp_mosquitto_disconnect_callback_set'
undefined reference to `__imp_mosquitto_message_callback_set'
undefined reference to `__imp_mosquitto_log_callback_set'
undefined reference to `__imp_mosquitto_connect'
...

I didn't expect such a quick response, so I opened a ticket in the mosquitto repo too.
eclipse/mosquitto#3035

@Biswa96
Copy link
Member

Biswa96 commented Apr 10, 2024

It is not an issue. You forgot to define LIBMOSQUITTO_STATIC macro for static linking. We could also add that macro in pkgconfig file.

@silabs-beta
Copy link
Author

@Biswa96 Shall I replace the -lmosquitto linker flag to -lmosquitto_static? Is this what you meant?
It doesn't work, either, I get the same undefined reference errors.

@Biswa96
Copy link
Member

Biswa96 commented Apr 10, 2024

I meant to define LIBMOSQUITTO_STATIC macro in compiler flag. For example, gcc -DLIBMOSQUITTO_STATIC -static test.c <put the rest things here>

@silabs-beta
Copy link
Author

I don't understand how could a compiler flag solve a linker issue.
Anyway, I tried it, and the original issue still persists: cannot find -lmosquitto: No such file or directory

@silabs-beta
Copy link
Author

@podsvirov , your fix renames libmosquitto_static.a to libmosquitto.a. But the libmosquitto.dll.a should be renamed to libmosquitto.a instead.

@Biswa96
Copy link
Member

Biswa96 commented Apr 10, 2024

But the libmosquitto.dll.a should be renamed to libmosquitto.a instead.

No. The .dll.a are import library and those are correctly named.

@silabs-beta
Copy link
Author

I agree, it can be correctly named in theory, but the experience shows that the fix doesn't work. If I rename the one with the .dll.a suffix, then the build succeeds.

@Biswa96
Copy link
Member

Biswa96 commented Apr 10, 2024

Could you provide any sample code to reproduce the issue?

@silabs-beta
Copy link
Author

Sure, you can use e.g. basic-1.c
and compile it like this: gcc basic-1.c -static -lmosquitto -o subscribe.exe

@Biswa96
Copy link
Member

Biswa96 commented Apr 10, 2024

Try this command gcc -static -DLIBMOSQUITTO_STATIC basic-1.c $(pkgconf -libs -static libmosquitto libssl)

The pkgconfig file is not correct yet. I shall try to upstream the changes first.

@silabs-beta
Copy link
Author

Indeed, I managed to build it (and found the reference to the LIBMOSQUITTO_STATIC in the API header).
Thank you for your support!

@silabs-beta
Copy link
Author

I have just tried the patched mosquitto artifacts from here.

Probably dumb question, but why do I need to add the libssl package manually when I use static linking? When use dynamic linking, then the following build command works perfectly:
gcc basic-1.c $(pkgconf -libs -cflags libmosquitto)
But the same command with the static flag fails because of undefined reference errors.
gcc -static basic-1.c $(pkgconf -libs -cflags -static libmosquitto)

Since there is no direct dependency on libssl in basic-1.c, I think this dependency should be provided in the libmosquitto.pc file too.
Probably a Requires.private: libssl or Libs.private: -lssl entry is missing?

@Biswa96
Copy link
Member

Biswa96 commented Apr 15, 2024

Probably a Requires.private: libssl or Libs.private: -lssl entry is missing?

Yes.

@silabs-beta
Copy link
Author

What should be the way forward? Shall I open a new issue for this one? Or Shall I open a PR myself?
And what is the process of adding the fixes in the original eclipse/mosquitto repo?

@Biswa96
Copy link
Member

Biswa96 commented Apr 15, 2024

I have added a new pull request to add the libssl in pkgconfig file of libmosquitto. I should have suggested the change in podsvirov's pull request. Would you like to review the new change please?

@podsvirov
Copy link
Collaborator

I have added a new pull request to add the libssl in pkgconfig file of libmosquitto. I should have suggested the change in podsvirov's pull request. Would you like to review the new change please?

Good addition.

@Biswa96
Copy link
Member

Biswa96 commented Apr 15, 2024

Please feel free to copy or modify or upstream my changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants