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

Unable to link against third party library like openssl #192

Open
dscreve opened this issue Mar 2, 2025 · 8 comments
Open

Unable to link against third party library like openssl #192

dscreve opened this issue Mar 2, 2025 · 8 comments

Comments

@dscreve
Copy link

dscreve commented Mar 2, 2025

Running an SDK on a Mac.

I'm linking against libcrypto (openSSL) and it does not work.

I have installed libssl-dev in docker Image as below :
RUN apt update && apt -y install libssl-dev && apt -y clean

But I don't know how to tell him to add it in the generated SDK and I have the following error :
ld.lld: error: undefined symbol: RAND_bytes
(and many others for the all call to libcrypto).

I have openSSL installed on the Mac, and It seems to try to look in the Mac library :
ld.lld: warning: /opt/homebrew/Cellar/openssl@3/3.0.7/lib/libssl.a: archive member 'libssl-lib-bio_ssl.o' is neither ET_REL nor LLVM bitcode

How to fix this ?

@xtremekforever
Copy link
Contributor

So my question is, how are you trying to link against libcrypto? Are you trying to use an -Xswiftc flag, or do you have it in your Package.swift as a systemLibrary?

@dscreve
Copy link
Author

dscreve commented Mar 2, 2025

I have a systemLibrary :

  .systemLibrary(
        name: "OpenSSL",
        pkgConfig: "openssl",
        providers: [
            .apt(["openssl libssl-dev"]),
            .brew(["openssl"]),
        ])

@xtremekforever
Copy link
Contributor

Okay, I did a little testing on my Linux system and found that if I include the pkgConfig: "openssl" line that swiftpm tries to look for -lssl on the host instead of in the target Swift SDK. So, if you remove that line, then it should start looking for libssl inside of the Swift SDK sysroot instead.

I didn't generate a Swift SDK with libssl-dev installed myself, but I got this result:

ld.lld: error: unable to find library -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Please give this a try!

@dscreve
Copy link
Author

dscreve commented Mar 2, 2025

This works fine. But there somethings strange when compiling on the mac using the SDK.

When compiling C source code with SDK, it seems to use local header and not headers in SDK and does not conform to header search path.
For example, it uses /opt/local/include/openssl/ossl_typ.h instead of /opt/homebrew/Cellar/openssl@3/3.0.7/include/openssl/ossl_typ.h and not the file in the SDK.

@t089
Copy link

t089 commented Mar 2, 2025

Try setting the pkg config search path. See here: #89 (comment)

@xtremekforever
Copy link
Contributor

xtremekforever commented Mar 3, 2025

On my end in Linux, looks like setting these paths causes pkgconfig to work properly with the Swift SDK:

export PKG_CONFIG_SYSROOT_DIR=~/.swiftpm/swift
-sdks/6.0.3-RELEASE_ubuntu_jammy_aarch64.artifactbundle/6.0.3-RELEASE_ubuntu_jammy_aarch64/aarch64-unknown-linux-gnu/ubuntu-jammy.sdk
export PKG_CONFIG_PATH=$PKG_CONFIG_SYSROOT_DIR/usr/lib/aarch64-linux-gnu/pkgconfig

The only problem is that this seems like it's a lot of work to set these variables just for pkgconfig to point to the Swift SDK directory. I wonder if this should be moved into SwiftPM to automatically set these when a --swift-sdk is used...or has it been in some way? swiftlang/swift-package-manager#7472

@euanh
Copy link
Contributor

euanh commented Mar 3, 2025

The only problem is that this seems like it's a lot of work to set these variables just for pkgconfig to point to the Swift SDK directory. I wonder if this should be moved into SwiftPM to automatically set these when a --swift-sdk is used...or has it been in some way? swiftlang/swift-package-manager#7472

This is swiftlang/swift-package-manager#7409

@dscreve
Copy link
Author

dscreve commented Mar 3, 2025

On my end in Linux, looks like setting these paths causes pkgconfig to work properly with the Swift SDK:

export PKG_CONFIG_SYSROOT_DIR=~/.swiftpm/swift
-sdks/6.0.3-RELEASE_ubuntu_jammy_aarch64.artifactbundle/6.0.3-RELEASE_ubuntu_jammy_aarch64/aarch64-unknown-linux-gnu/ubuntu-jammy.sdk
export PKG_CONFIG_PATH=$PKG_CONFIG_SYSROOT_DIR/usr/lib/aarch64-linux-gnu/pkgconfig

The only problem is that this seems like it's a lot of work to set these variables just for pkgconfig to point to the Swift SDK directory. I wonder if this should be moved into SwiftPM to automatically set these when a --swift-sdk is used...or has it been in some way? swiftlang/swift-package-manager#7472

This does not work when host is macOS. it still continues to use the macOS header : I use the function EVP_MD_get_type() which is declared in ubuntu-jammy.sdk/usr/include/openssl/evp.h but it tried to open /usr/local/include/openssl/evp.h on macOS, which does not contains declaration. It seems to ignore openssl.pc configuration.

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

No branches or pull requests

4 participants