Call OpenImageIO from swift

I call OpenImageIO (C++) from Swift and everything works fine except linking:

  guard let textureSystem = OIIO.TextureSystem.create(true, nil) else {
                  exit(0)
  }
  let buffer = UnsafeMutablePointer<Float>.allocate(capacity: 3)
  successful = textureSystem.pointee.texture(
          filename, &options, s, t, dsdx, dtdx, dsdy, dtdy,
          nchannels, buffer, nil, nil)

The error:

error: link command failed with exit code 1 (use -v to see invocation)
../Texture/OpenImageIO.swift.o:OpenImageIO.swift.o:function $s8gonzales3blayyF: Error undefined reference to 'OpenImageIO_v2_3::TextureSystem::texture(OpenImageIO_v2_3::ustring, OpenImageIO_v2_3::TextureOpt&, float, float, float, float, float, float, int, float*, float*, float*)'
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)
[0/1] Linking ...

nm Texture/OpenImageIO.swift.o gives:

U _ZN16OpenImageIO_v2_313TextureSystem7textureENS_7ustringERNS_10TextureOptEffffffiPfS4_S4_

strings /usr/lib/x86_64-linux-gnu/libOpenImageIO.so|grep texture|grep TextureSystem|grep ustring gives

_ZN16OpenImageIO_v2_33pvt17TextureSystemImpl16get_texture_infoENS_7ustringEiS2_NS_8TypeDescEPv
_ZN16OpenImageIO_v2_33pvt17TextureSystemImpl16get_texture_infoEPNS_13TextureSystem13TextureHandleEPNS2_9PerthreadEiNS_7ustringENS_8TypeDescEPv
_ZN16OpenImageIO_v2_33pvt17TextureSystemImpl7textureENS_7ustringERNS_10TextureOptEffffffiPfS5_S5_
_ZN16OpenImageIO_v2_33pvt17TextureSystemImpl7textureENS_7ustringERNS_15TextureOptBatchEmPKfS6_S6_S6_S6_S6_iPfS7_S7_
_ZN16OpenImageIO_v2_33pvt17TextureSystemImpl7textureENS_7ustringERNS_14TextureOptionsEPhiiNS_10VaryingRefIfEES7_S7_S7_S7_S7_iPfS8_S8_
_ZN16OpenImageIO_v2_33pvt17TextureSystemImpl9texture3dENS_7ustringERNS_10TextureOptERKN9Imath_3_14Vec3IfEES9_S9_S9_iPfSA_SA_SA_
_ZN16OpenImageIO_v2_33pvt17TextureSystemImpl9texture3dENS_7ustringERNS_14TextureOptionsEPhiiNS_10VaryingRefIN9Imath_3_14Vec3IfEEEESA_SA_SA_iPfSB_SB_SB_
_ZN16OpenImageIO_v2_33pvt17TextureSystemImpl9texture3dENS_7ustringERNS_15TextureOptBatchEmPKfS6_S6_S6_iPfS7_S7_S7_

I note the difference "_v2_313" vs "_v2_33" in the name mangling.
How to get rid of this error?

Everything else in Package.swift, module.modulemap seems to be fine.

Seems to be related to [interop] Dispatch virtual method calls correctly · Issue #62354 · apple/swift · GitHub

Yes, that's the correct bug, we currently don't dispatch virtual calls correctly and this appears to be such case.

note that you can workaround this by creating an inline function in C++ that wraps around the original call to virtual function and call such inline function instead.

1 Like