Include C static library into my windows swift project and build executable


i have created C static library with simple function declared in header and implemented in .c file


int mylibfnc(); (in .h file)


.c file:
int mylibfnc()
return 123;

compiled -> static mylib.lib has been created

now how to allow calling of this function in my .swift file?
i tried analogically (as other libs in existing C:\Library folder created by windows swift installator) to copy header file and create
module MYLIB [extern_c] [system] {
umbrella header "mylib.h"
at location:
(as modul declaration)
add my mylib.lib at location c:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\windows\x86_64
assuming that lib is being "registred" and inner function becomes usable after
import MYLIB in my swift file

after using
set SWIFTFLAGS=-sdk %SDKROOT% -resource-dir %SDKROOT%\usr\lib\swift -I %SDKROOT%\usr\lib\swift -L %SDKROOT%\usr\lib\swift\windows


swiftc %SWIFTFLAGS% -emit-executable -o helloWorld.exe helloWorld.swift

there is compilation error of unresolved external symbol mylibfnc

could you please help me to run the most simple application?

let's say, i have simple helloWorld.swift
how to run function from static lib compiled by visual studio?
where should i copy .h file and .lib file, how to let swift now about .lib implementation?

has to be next to my swift file some parametrized json with specifications?
could you offer little example pls?

thank you very much

If you are looking for an example of how to handle a mixed library project, I would recommend that you take a look at swift-cmake-examples/HelloWorld at main · compnerd/swift-cmake-examples · GitHub

1 Like

thank you for your answer!

one more question: what if i have no .c file, only header file (.h) and static library (public .lib) compiled in windows?
in the example the source code CCoreVersion.c is expected and compiled ad hoc by cmake

important note - the final executable will be on windows only

thank you

If you are intending to do that with CMake, that is pretty easy to accommodate. I can try to create a small sample for that, but, you should be able to look up documentation for it with the term "imported library".

Keep in mind that SPM does not really support that outside of Darwin atm.