This might be a good time for an update from my side.
First of all, I would also assume that "objc" is just some kind of poor naming. Nevertheless, i was only talking abot "bare" C code so i did not check if Objective-C would work also. However, regarding the usability on other platforms than Apples Darwin we had some drawbacks yesterday by adding more of our C stuff to the bridging header.
The example above works flawlessly on all patforms independend from the enrionment which had been used. The C-struct can be imported and used how its intended.
One note may be, that on Windows, we had to change the #import
statement in the bridging header to #include
as the compiler told us that "#import of type library is an unsupported Microsoft feature
".
However, as soon as we add c functions it does work only using Xcode. Every attempt to use CMake fails, even on Darwin as linking fails due to an unresolved external symbol referring to the functions name. Like said before, doing this inside Xcode is fine.
Somehow we don't feel like giving up yet, but then again maybe this is proof that this is inteded to be used just on Darwin.
Just for completion, if someone wants to give it a try:
cct.c
// cct.c
#include "cct.h"
int doublemyint(int a) {
return a*2;
};
cct.h
// cct.h
#ifndef cct_h
#define cct_h
struct astruct {
int a;
};
int doublemyint(int a);
#endif /* cct_h */
sctest.swift
// sctest.swift
import Foundation
var astr=astruct()
astr.a = 1
print(astr)
var a:CInt
var b:CInt
a=2
b=doublemyint(a)
sctest-Bridging-Header.h
#include "cct.h"
CMakeLists.txt
CMAKE_minimum_required( VERSION 3.20 )
enable_language(Swift)
project( SwiftTest VERSION 0.1.0 )
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin")
file( GLOB_RECURSE main_src ${CMAKE_CURRENT_SOURCE_DIR}/sctest.swift)
add_executable(sctest ${main_src})
target_compile_options(sctest PRIVATE -import-objc-header ${CMAKE_CURRENT_SOURCE_DIR}/sctest-Bridging-Header.h)