# Is there a way to emit C/C++ header?

**URL:** <https://forums.swift.org/t/is-there-a-way-to-emit-c-c-header/71621>\
**Category:** Using Swift\
**Tags:** c-interop\
**Created:** [May 5, 2024, 2:39am UTC](https://forums.swift.org/t/is-there-a-way-to-emit-c-c-header/71621 "2024-05-05T02:39:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dima\_kozhinov](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dima\_kozhinov](https://forums.swift.org/u/dima_kozhinov)\
**Post date:** [May 5, 2024, 2:39am UTC](https://forums.swift.org/t/is-there-a-way-to-emit-c-c-header/71621/1 "2024-05-05T02:39:04Z")

</div>

When building a dynamic library intended to be used with a host application written in C/C++, is there any way to generate a C/C++ header file?

A public function can be declared with the `@_cdecl("functionName")` attribute and have a C calling convention.

The `swiftc` compiler can be invoked with the `-emit-objc-header` command line argument, and in this case a header file is created, but it's an Objective-C header file, and I believe it's not compatible with C/C++.

Now that C/C++ interoperability is actively developing, is there a way to emit a C/C++ header file?

---

<div class="post-metadata">

**Author:** ![wadetregaskis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wadetregaskis/32/8494_2.png) [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Post date:** [May 5, 2024, 3:51am UTC](https://forums.swift.org/t/is-there-a-way-to-emit-c-c-header/71621/2 "2024-05-05T03:51:09Z")

</div>

I've been wondering this too, a bit. It'd be nice to be able (eventually) to produce good-quality C++ APIs atop Swift code. I like to think it opens some doors for using Swift for core implementations while supporting C++ users, rather than the other way around.

---

<div class="post-metadata">

**Author:** ![ole](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ole/32/18720_2.png) [@ole](https://forums.swift.org/u/ole)\
**Post date:** [May 5, 2024, 5:57am UTC](https://forums.swift.org/t/is-there-a-way-to-emit-c-c-header/71621/3 "2024-05-05T05:57:26Z")

</div>

Quoting [Setting Up Mixed-Language Swift and C++ Projects](https://www.swift.org/documentation/cxx-interop/project-build-setup/#mixing-swift-and-c-using-other-build-systems) from [swift.org](http://swift.org):

> The `-emit-clang-header-path` Swift frontend flag can be used to emit a generated header when exposing Swift APIs to C++ when building Swift code in a build system that doesn’t provide automatic support for generating a header file with exposed APIs.
> 
> The following Swift compiler invocation emits a generated header file for the `SwiftModule` module that consists of two source files, `a.swift` and `b.swift`:
> 
> ```sh
> swiftc -frontend -typecheck \
> /sources/a.swift /sources/b.swift -module-name SwiftModule \
> -cxx-interoperability-mode=default \
> -emit-clang-header-path SwiftModule-Swift.h
> 
> ```

I haven’t tried this.

---

<div class="post-metadata">

**Author:** ![dima\_kozhinov](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dima\_kozhinov](https://forums.swift.org/u/dima_kozhinov)\
**Post date:** [May 5, 2024, 9:47pm UTC](https://forums.swift.org/t/is-there-a-way-to-emit-c-c-header/71621/4 "2024-05-05T21:47:11Z")

</div>

It works. The `-emit-clang-header-path` does the trick.  
But there are concerns, at least for me:

1. This command line parameter seems to be undocumented. The `swiftc --help` command does not mention it.
2. The `-cxx-interoperability-mode=default` command line parameter is required to emit Objective C/C/C++ compatible header file. Without it only Objective C header is emitted.

Why I'm uncomfortable with the C++ interoperability flag is because modules built with this flag used to be binary incompatible with modules built without it (they seem to be compatible as of Swift 5.10, which is great). And I tend to import precompiled modules (as dynamic libraries) rather than recompile all dependencies every time. Thanks a lot anyway @ole!

---

<div class="post-metadata">

**Author:** ![carlynorama](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlynorama/32/27072_2.png) [@carlynorama](https://forums.swift.org/u/carlynorama)\
**Post date:** [July 12, 2025, 2:06am UTC](https://forums.swift.org/t/is-there-a-way-to-emit-c-c-header/71621/5 "2025-07-12T02:06:47Z")

</div>

If anyone needs it I have a minimal CMake version of this working in a tiny mixed C and Swift example, thanks to this post and [an article that referenced it](https://christiantietze.de/posts/2025/04/calling-swift-library-functions-from-c/)

One warning, you currently have to build it twice for the emitted header to be there for the main build, but I'm working on figuring out artifact dependencies. (Apparently it might be a Ninja thing)

> **[another-swift-cmake-examples/CInterop/SwiftLib\_CTarget at main ·...](https://github.com/carlynorama/another-swift-cmake-examples/tree/main/CInterop/SwiftLib_CTarget)**
>
> Contribute to carlynorama/another-swift-cmake-examples development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![dima\_kozhinov](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dima\_kozhinov](https://forums.swift.org/u/dima_kozhinov)\
**Post date:** [July 29, 2025, 12:45pm UTC](https://forums.swift.org/t/is-there-a-way-to-emit-c-c-header/71621/6 "2025-07-29T12:45:23Z")

</div>

The C++ header is generated, but nowadays it has become so huge that I have doubts that is will compile without problems on C++ side.

Let me explain what I mean:  
If my Swift-written dynamic library exports just one function (has just one `public func`) and does not use Swift specific types as its parameters or a return value (e.g. uses just `Int`), the emitted header nevertheless contains thousands lines of code related to some esoteric C++ interop and not related to my function at all. Earlier versions of Swift did not do that.

Example:  
My library contains 4 lines of code:

```swift
@_cdecl("meaningOfLife")
public func meaningOfLife() -> Int {
    return 42
}

```

The emitted header for my library contains 3975 lines of code (Swift 6.1.2, Windows platform). I could not even paste it here, as Discourse has a limit on number of lines in one post.
