-
Notifications
You must be signed in to change notification settings - Fork 16
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
Comments
So my question is, how are you trying to link against libcrypto? Are you trying to use an |
I have a systemLibrary :
|
Okay, I did a little testing on my Linux system and found that if I include the I didn't generate a Swift SDK with
Please give this a try! |
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. |
Try setting the pkg config search path. See here: #89 (comment) |
On my end in Linux, looks like setting these paths causes pkgconfig to work properly with the Swift SDK:
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. |
I was able to build a binary on macOS and run it on Linux using the following steps. It seems to have found and linked OpenSSL and a very basic function call seems to return reasonable output. Does this work for you? Build the container imageFROM swift:6.0.3-bookworm
RUN apt update && apt -y install openssl libssl-dev pkg-config && apt -y clean It's not necessary to install
Build the SDKOn a macOS host (aarch64, in this case), run SDK generator to extract the SDK. If you are using the OSS toolchain on macOS,
Note: I simplified this command by removing Install the SDK
Prepare a demo project
// swift-tools-version: 6.0
import PackageDescription
let package = Package(
name: "openssl-test",
targets: [
.executableTarget(
name: "openssl-test",
dependencies: ["OpenSSL"]),
.systemLibrary(
name: "OpenSSL",
pkgConfig: "openssl",
providers: [
.apt(["openssl libssl-dev"]),
.brew(["openssl"]),
])
]
)
import OpenSSL
guard let sha256 = EVP_MD_fetch(nil, "sha256", nil) else {
fatalError("failed to fetch sha256")
}
print("sha256 ptr: \(sha256)")
let oid = EVP_MD_get_type(sha256)
print("sha256 oid: \(oid)")
print("expected NID_sha256: \(NID_sha256)") // defined in 6.0.3-bookworm-arm64/aarch64-unknown-linux-gnu/ubuntu-jammy.sdk/usr/include/openssl/obj_mac.h
module OpenSSL [system] {
header "openssl_shim.h"
export *
}
// openssl.h
#pragma once
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h> Build the binary using the SDK⚠ Please double-check that
I'm using I'm setting I do have a copy of openssl installed by
To make sure that the headers from
This implies that these headers are not being used for the successful cross-build above. Run the binary on LinuxOn Ubuntu aarch64:
|
I did exactly what you describe (except the use of -disable-round-trip-debug-types that do not cause issue while compiling dk-generator), and It does not work. % swift build --swift-sdk 6.0.3-bookworm-arm64 --static-swift-stdlib |
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 ?
The text was updated successfully, but these errors were encountered: