Hello
I'm trying to link a dll that was compiled within msvc to get symbols and use in swift
However when I try to compile I get the following error
lld-link: warning: C:\Users\mohamed\projects\ls.build\x86_64-unknown-windows-msvc\debug\ls.build\main.swift.o: locally defined symbol imported: $s10swift_tolk8lib_loadyyF (defined in C:\Users\mohamed\projects\ls.build\x86_64-unknown-windows-msvc\debug\swift_tolk.build\swift_tolk.swift.o) [LNK4217]
lld-link: error: undefined symbol: __declspec(dllimport) Tolk_Load
referenced by C:\Users\mohamed\projects\ls.build\x86_64-unknown-windows-msvc\debug\swift_tolk.build\swift_tolk.swift.o:($s10swift_tolk8lib_loadyyF)
lld-link: error: undefined symbol: __declspec(dllimport) Tolk_Unload
referenced by C:\Users\mohamed\projects\ls.build\x86_64-unknown-windows-msvc\debug\swift_tolk.build\swift_tolk.swift.o:($s10swift_tolk8lib_loadyyF)
lld-link: error: undefined symbol: __declspec(dllimport) Tolk_Speak
referenced by C:\Users\mohamed\projects\ls.build\x86_64-unknown-windows-msvc\debug\swift_tolk.build\swift_tolk.swift.o:($s10swift_tolk8lib_loadyyFSbSPys6UInt16VGXEfU_)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The code is
import windows
public func lib_load() {
Tolk_Load()
let str = "Hello, World".utf16
var thing: UInt16 = 0
for i in str {
thing += i
}
withUnsafePointer(to: thing) {
Tolk_Speak($0, true)
}
Tolk_Unload()
}
module.modulemap
module windows [system][extern_c] {
umbrella header "Tolk.h"
export *
}
Tolk.h
#ifndef TOLK_H
#define TOLK_H
#ifdef _EXPORTING
#define TOLK_DLL_DECLSPEC __declspec(dllexport)
#else
#define TOLK_DLL_DECLSPEC __declspec(dllimport)
#endif // _EXPORTING
#define TOLK_CALL __cdecl
#ifdef __cplusplus
extern "C" {
#else
#include <stdbool.h>
#include <wchar.h>
#endif // __cplusplus
TOLK_DLL_DECLSPEC void TOLK_CALL Tolk_Load();
TOLK_DLL_DECLSPEC void TOLK_CALL Tolk_Unload();
#ifdef __cplusplus
TOLK_DLL_DECLSPEC bool TOLK_CALL Tolk_Speak(const wchar_t *str, bool interrupt = false);
#else
TOLK_DLL_DECLSPEC bool TOLK_CALL Tolk_Speak(const wchar_t *str, bool interrupt);
#endif // __cplusplus
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#endif // TOLK_H
Package.swift
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "swift_tolk",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "swift_tolk",
targets: ["swift_tolk"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
.systemLibrary (
name: "Cwindows_bind"),
.target(
name: "swift_tolk",
dependencies: ["Cwindows_bind"], cxxSettings: [.unsafeFlags(["-LTolk.dll"])]),
]
)
And I call this by doing
import tolk_swift
tolk_swift.lib_load()
So, Am I doing something wrong?