# How do I Add C++ Files to Embedded Swift Project

**URL:** <https://forums.swift.org/t/how-do-i-add-c-files-to-embedded-swift-project/74529>\
**Category:** Embedded\
**Created:** [September 11, 2024, 8:37pm UTC](https://forums.swift.org/t/how-do-i-add-c-files-to-embedded-swift-project/74529 "2024-09-11T20:37:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaulSolt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paulsolt/32/16477_2.png) [@PaulSolt](https://forums.swift.org/u/PaulSolt)\
**Post date:** [September 11, 2024, 8:37pm UTC](https://forums.swift.org/t/how-do-i-add-c-files-to-embedded-swift-project/74529/1 "2024-09-11T20:37:13Z")

</div>

How do I add C or C++ files to the project that can be used in Embedded Swift (ESP32)?

I'm exploring the Embedded Swift Sample Projects (ESP32 LED Strip) [esp32-led-strip-sdk](https://github.com/apple/swift-embedded-examples/tree/main/esp32-led-strip-sdk)

I have existing code that I want to add, but I haven't been able to get Embedded Swift to compile with non-Swift C/C++ code.

In the `CMakeLists.txt` it looks like I need to modify two areas:

```cmake
# Register the app as an IDF component
idf_component_register(
    SRCS /dev/null # We don't have any C++ sources
    PRIV_INCLUDE_DIRS "."
    LDFRAGMENTS "linker.lf"
)

```

and

```cmake
# List of Swift source files to build.
target_sources(${COMPONENT_LIB}
    PRIVATE
    Main.swift
    LedStrip.swift
)

```

I attempted to expose it, but I don't really understand how it needs to be formatted with CMake and ESP IDF.

**BridgingHeader.h**

```objc
#include "print_float.h"

```

**CMakeLists.txt**

```cmake
# List of Swift source files to build.
target_sources(${COMPONENT_LIB}
    PRIVATE
    src/print_float.cpp
    Main.swift
    LedStrip.swift
    Format.swift
)

```

I get errors as it is not seeing the C++ function: `undefined reference to printFloat`

```swift
[1/6] Performing build step for 'bootloader'
[1/1] cd /Users/paulsolt/dev/embedded/LED/build/bootloader/esp-idf/esptool_py && /Users/paulsolt/.espressif/python_env/idf5.2_py3.12_env/bin/python /Users/paulsolt/esp/esp-idf/components/partition_table/check_sizes.py --offset 0x8000 bootloader 0x0 /Users/paulsolt/dev/embedded/LED/build/bootloader/bootloader.bin
Bootloader binary size 0x54b0 bytes. 0x2b50 bytes (34%) free.
[4/6] Linking CXX executable main.elf
FAILED: main.elf 

...

/Users/paulsolt/.espressif/tools/riscv32-esp-elf/esp-13.2.0_20230928/riscv32-esp-elf/bin/../lib/gcc/riscv32-esp-elf/13.2.0/../../../../riscv32-esp-elf/bin/ld: esp-idf/main/libmain.a(Main.swift.obj): in function `.Lpcrel_hi1':
Main.swift.obj:(.text.$s10__idf_main04app_B0yyF+0x3c): undefined reference to `printFloat'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

```

---

<div class="post-metadata">

**Author:** ![PaulSolt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paulsolt/32/16477_2.png) [@PaulSolt](https://forums.swift.org/u/PaulSolt)\
**Post date:** [September 12, 2024, 12:39am UTC](https://forums.swift.org/t/how-do-i-add-c-files-to-embedded-swift-project/74529/2 "2024-09-12T00:39:38Z")

</div>

Ok I figured out how to add C files to my project, but it looks like I need to add a separate step to compile C++ files into an object file before they can be linked with Swift.

I haven't figured out C++ code yet ...

I added two folders under my main folder:

```swift
main/
    include/
         print_float.h
    src/
         print_float.c

```

Then I updated the CMakeLists.txt to grab all the source files in the folder, so that I don't need to keep updating the CmakeLists.txt if I add files.

### print\_float.h

```cc
void printFloat(float value);

```

### print\_float.c

```cc
#ifndef PRINT_FLOAT_H
#define PRINT_FLOAT_H

#include "print_float.h"
#include <stdio.h>

void printFloat(float value) {
    printf("%f\n", value);
}

#endif

```

## main/CMakeLists.txt

```swift
file(GLOB_RECURSE SOURCES "src/*.c")
message(SOURCES="${SOURCES}") # Prints out the source files added

idf_component_register(
    # SRCS /dev/null # We don't have any C++ sources
    SRCS ${SOURCES} 
    INCLUDE_DIRS "include"
    PRIV_INCLUDE_DIRS "."
    LDFRAGMENTS "linker.lf"
)

```

---

<div class="post-metadata">

**Author:** ![rauhul](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rauhul/32/35207_2.png) [@rauhul](https://forums.swift.org/u/rauhul)\
**Post date:** [September 12, 2024, 3:54am UTC](https://forums.swift.org/t/how-do-i-add-c-files-to-embedded-swift-project/74529/3 "2024-09-12T03:54:31Z")

</div>

I dont have any pointers on how to do this, but whatever you do figure out would be good to push back to embedded examples so others can follow your lead!

---

<div class="post-metadata">

**Author:** ![PaulSolt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paulsolt/32/16477_2.png) [@PaulSolt](https://forums.swift.org/u/PaulSolt)\
**Post date:** [September 12, 2024, 10:35am UTC](https://forums.swift.org/t/how-do-i-add-c-files-to-embedded-swift-project/74529/4 "2024-09-12T10:35:24Z")

</div>

@rauhul I attempted to use these guides for Swift and C++, but I don't understand how the ESP32 IDF components and Swift/C++ build settings mix together.

I attempted to merge the CMakeLists.txt settings, but I couldn't get anything to build.

1. [Creating a Module to contain your C++ source code (Building with CMake)](https://github.com/swiftlang/swift/blob/main/docs/CppInteroperability/GettingStartedWithC%2B%2BInterop.md#creating-a-module-to-contain-your-c-source-code)
2. [Setting Up Mixed-Language Swift and C++ Projects](https://www.swift.org/documentation/cxx-interop/project-build-setup/#generating-c-header-with-exposed-swift-apis)

That second link makes it sound like I just need the `-cxx-interoperability-mode=default` build setting to make it build with `swiftc`, but the module map and layout of the C++ code may still be important from the previous link.

---

<div class="post-metadata">

**Author:** ![rauhul](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rauhul/32/35207_2.png) [@rauhul](https://forums.swift.org/u/rauhul)\
**Post date:** [September 12, 2024, 5:23pm UTC](https://forums.swift.org/t/how-do-i-add-c-files-to-embedded-swift-project/74529/5 "2024-09-12T17:23:05Z")

</div>

Hmm yeah this is tricky because you're running into the combination of:

- C++ Interop
- CMake Swift
- ESP Specific CMake

I dont actually think Embedded Swift play a large role in this mix ironically.

Maybe @kubamracek might know how to setup the CMake for this, or perhaps @etcwilde

---

<div class="post-metadata">

**Author:** ![rrroyal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rrroyal/32/39685_2.png) [@rrroyal](https://forums.swift.org/u/rrroyal)\
**Post date:** [February 23, 2025, 11:26pm UTC](https://forums.swift.org/t/how-do-i-add-c-files-to-embedded-swift-project/74529/6 "2025-02-23T23:26:21Z")

</div>

any luck figuring it out?

as a temporary solution - implement the function in a .h file, wrap it in `#ifndef _YOUR_H_FILE_ #define _YOUR_H_FILE_` and include it in the bridging header 😶‍🌫️

---

<div class="post-metadata">

**Author:** ![PaulSolt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paulsolt/32/16477_2.png) [@PaulSolt](https://forums.swift.org/u/PaulSolt)\
**Post date:** [March 10, 2025, 2:22am UTC](https://forums.swift.org/t/how-do-i-add-c-files-to-embedded-swift-project/74529/7 "2025-03-10T02:22:21Z")

</div>

That was the only thing I was able to figure out. I haven't had any time to play again with Embedded Swift.
