I want to create a simple C++ executable that just calls a Swift function from the C++ main.
So just something like add_executable(Interop main.cpp func.swift).
In Xcode this is easy, but I can't figure out how to get this to work with CMake. I used Apple's CMake example and the CMake modules they provided, but I get the error that _main is a duplicate symbol, probably because a separate main is created for the Swift file.
My example CMake looks like this, where _swift_generate_cxx_header is the function from Apple's example.
add_executable(Interop main.cpp func.swift)
target_compile_options(Interop PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default>")
_swift_generate_cxx_header(Interop Interop/Interop-swift.h)
Swift will try to treat single swift sources as top-level-code. Try passing -parse-as-library to the swift compiler and you shouldn’t see that anymore.
The error message shows the swiftc invocation with -emit-executable, that sounds also potentially problematic, but I don't see where that is coming from.
What did I do wrong?
to control the namespace of my Swift functions. In Xcode the namespace defaults to the target name, which can cause problems if there are multiple targets like an executable and a gtest target.
Is there an equivalent Xcode options to control the namespace?