Skip to content

shared_ptr object creation is not working between swift/c++ interoperability. #67355

@asheeshsoni01

Description

@asheeshsoni01

Description
Creating a shared_ptr object in the swift file is not working. It is stated in the documentation that shared_ptr functionality is supported but it is not working.

C++ header file: Sample.h

#ifndef Sample_h
#define Sample_h

#include
#include

inline std::shared_ptr makeSharedData(int data) {
return std::make_shared (data);
}

class sample {
public:
sample(std::shared_ptr data);
int read_data();
void write_data(int data);
void ref_func(int& data);
private:
std::shared_ptr m_data;
};

#endif /* Sample_h */

Respective Sample.cpp file :

#include "Sample.h"

sample::sample(std::shared_ptr data) {
m_data = data;
}

int sample::read_data() {
return *m_data;
}

void sample::write_data(int data) {
*m_data = data;
}

void sample::ref_func(int& data) {
data = 100;
}

Swift File Contents : TestFile.swift

import Foundation

func connectCPP() -> Int32 {
//1. making use of the inline function in header file and calling that to get the shared_ptr object which in turns used to make the
// class object. This is throwing below error :
/* std::__1::__shared_ptr_emplace<int, std::__1::allocator>::__shared_ptr_emplace(), referenced from:
vtable for std::__1::__shared_ptr_emplace<int, std::__1::allocator> in TestFile.o
std::__1::__shared_ptr_emplace<int, std::__1::allocator>::
__shared_ptr_emplace(), referenced from:
vtable for std::__1::__shared_ptr_emplace<int, std::__1::allocator> in TestFile.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
*/
var obj = sample(makeSharedData(30))

//2. If I am trying to use make_shared directly in swift file, It is throwing error of symbol not found for make_shared and
// shared_ptr.

var data = std::make_shared<int>(10)
var obj1 = sample(data)
obj.write_data(12)
return obj.read_data()

}

If shared_ptr support is present in current version of interoperability, then it should be working fine without any error.

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.c++ interopFeature: Interoperability with C++c++ to swiftFeature → c++ interop: c++ to swiftcompilerThe Swift compiler itselflinker errorswift 6.0

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions