foo.swift
@_cdecl("foo")
public func foo(x: Int) -> Int {
x * x
}
The @_cdecl
will disable name mangling and enforces cdecl-calling convention.
main.cpp
#include <iostream>
extern "C" int foo(int);
int main()
{
int x = foo(8);
std::cout << x << std::endl;
}
Build commands
swiftc -emit-library foo.swift
clang++ -o main main.cpp libfoo.dylib
Links: