Using C++ Struct (containing arrays) in Swift

Reproducible demo Swift Package on github

I'm trying to use this c++ struct in swift (within a package)

#ifndef CPPARRAYLIB_H
#define CPPARRAYLIB_H

#include <vector>

typedef struct ArrayValues {
    std::vector<double> doubleValues;
    std::vector<unsigned int> intValues;
} ArrayValues;

#endif

But I get the errors:

error: multiple definitions of symbol '$sSo1soiySiSo3stdO3__1O0020___wrap_iter__udAAdDaV_AGtFTO'
error: multiple definitions of symbol '$sSo2eeoiySbSo3stdO3__1O0020___wrap_iter__udAAdDaV_AGtFTO'
error: multiple definitions of symbol '$sSo3stdO3__1O0020___wrap_iter__udAAdDaV9successorAFyF'
error: multiple definitions of symbol '$sSo3stdO3__1O0020___wrap_iter__udAAdDaV2peoiyyAFz_SitFZ'

when calling it in swift

import cppArrayLib

struct MyArray {
    
    let doubleValues: [Double]
    
    let intValues: [UInt32]
    
    init(arrayValues: ArrayValues) {
        self.doubleValues = Array<Double>(arrayValues.doubleValues)
        self.intValues = Array<UInt32>(arrayValues.intValues)
    }
}

If I only access one of the arrays inside arrayValues, it compiles as expected.

    init(arrayValues: ArrayValues) {
        self.doubleValues = Array<Double>(arrayValues.doubleValues)
        self.intValues = []
    }

What's causing these "multiple definitions of symbol" error, and how do I solve it?


Versions

  • Xcode 15.4
  • Swift 5.10

This is a compiler bug that was recently fixed. Could you please try using a development snapshot compiler to get access to the fixed compiler?

Thanks Egor. The code successfully compiles using

Swift Development Sanpshot 2024-11-14 (a)

1 Like