Aktualnie konwertuję moje instancje auto_ptr
na unique_ptr
, ale mam problem. Działa wspaniale w części kodu C++, ale gdy robię to w mojej zarządzanej warstwie C++/CLI (oprogramowanie wykorzystuje zarówno C#, jak i C++), otrzymuję błędy łącza. Kompiluje się dobrze, ale zrywa czas połączenia. Nigdy nie było żadnych problemów z auto_ptr
.Błąd łącznika podczas korzystania z unique_ptr w języku C++/CLI
Aktualnie używam programu Visual Studio 2010. Czy ktoś wie o jakichkolwiek problemach z używaniem unique_ptr
w C++/CLI?
Próbowałem podsumować mój problem w kawałku kodu poniżej, ale należy pamiętać, że kod poniżej faktycznie kompiluje i działa (sprawdziłem, czy własność wskaźników została poprawnie przeniesiona). Podczas kompilacji nie pojawiają się błędy łączące, ale poniższy kod to czysty C++, a nie C++/CLI. Chciałem mieć tylko minimalny przykład konstrukcji kodu, więc błąd linkera ma sens.
#include "stdafx.h"
#include <vector>
#include <memory>
#include <utility>
using namespace std;
namespace Test {
template< class T >
struct LinAlgPoint3 {
LinAlgPoint3() { x = y = z = 0; };
union {
struct {T x,y,z;} ;
T data_[3];
};
};
class ContainerClass
{
public:
void setUniquePtr(
unique_ptr< vector< LinAlgPoint3<float> > > newUniquePtr1 ,
unique_ptr< vector< unsigned char > > newUniquePtr2)
{
m_uniquePtr1 = move(newUniquePtr1);
m_uniquePtr2 = move(newUniquePtr2);
}
private:
unique_ptr< vector< LinAlgPoint3<float> > > m_uniquePtr1;
unique_ptr< vector< unsigned char > > m_uniquePtr2;
};
int main(int argc, char** argv)
{
auto pos = unique_ptr< vector< LinAlgPoint3<float> > >(new vector< LinAlgPoint3<float> >());
auto name = unique_ptr< vector< unsigned char > >(new vector< unsigned char >());
ContainerClass container;
container.setUniquePtr(move(pos), move(name));
}
} //namespace Test
Błąd pojawia podczas łączenia jest następujący:
error LNK2028: unresolved token (0A0018A5) "private: __cdecl std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > >::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > >(class std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > > const &)" ([email protected][email protected][email protected]@[email protected]@[email protected][email protected]@[email protected]@@[email protected]@@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected][email protected]@[email protected]@@[email protected]@@[email protected]@@[email protected]@[email protected]@[email protected]@@Z) referenced in function "public: static void __clrcall std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > >::<MarshalCopy>(class std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > > *,class std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > > *)" (?<MarshalCopy>@[email protected][email protected][email protected]@[email protected]@[email protected][email protected]@[email protected]@@[email protected]@@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected][email protected]@[email protected]@@[email protected]@@[email protected]@@[email protected]@[email protected]@[email protected]@Z)
1>TestClass.obj : error LNK2028: unresolved token (0A0018A6) "private: __cdecl std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > >::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > >(class std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > > const &)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected][email protected]@[email protected]@@[email protected]@@[email protected]@[email protected]@[email protected]@@Z) referenced in function "public: static void __clrcall std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > >::<MarshalCopy>(class std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > > *,class std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > > *)" (?<MarshalCopy>@[email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected][email protected]@[email protected]@@[email protected]@@[email protected]@[email protected]@[email protected]@Z)
1>TestClass.obj : error LNK2019: unresolved external symbol "private: __cdecl std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > >::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > >(class std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > > const &)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected][email protected]@[email protected]@@[email protected]@@[email protected]@[email protected]@[email protected]@@Z) referenced in function "public: static void __clrcall std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > >::<MarshalCopy>(class std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > > *,class std::unique_ptr<class std::vector<unsigned char,class std::allocator<unsigned char> >,struct std::default_delete<class std::vector<unsigned char,class std::allocator<unsigned char> > > > *)" (?<MarshalCopy>@[email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected][email protected]@[email protected]@@[email protected]@@[email protected]@[email protected]@[email protected]@Z)
1>TestClass.obj : error LNK2019: unresolved external symbol "private: __cdecl std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > >::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > >(class std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > > const &)" ([email protected][email protected][email protected]@[email protected]@[email protected][email protected]@[email protected]@@[email protected]@@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected][email protected]@[email protected]@@[email protected]@@[email protected]@@[email protected]@[email protected]@[email protected]@@Z) referenced in function "public: static void __clrcall std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > >::<MarshalCopy>(class std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > > *,class std::unique_ptr<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > >,struct std::default_delete<class std::vector<struct LinAlgPoint3<float>,class std::allocator<struct LinAlgPoint3<float> > > > > *)" (?<MarshalCopy>@[email protected][email protected][email protected]@[email protected]@[email protected][email protected]@[email protected]@@[email protected]@@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected][email protected]@[email protected]@@[email protected]@@[email protected]@@[email protected]@[email protected]@[email protected]@Z)
1>D:\Test\Test.dll : fatal error LNK1120: 4 unresolved externals
Jak widać (jeśli można uzyskać poprzez niezwykle strasznych wiadomości) istnieją pewne odniesienia do MarshalCopy, coś, co sprawia mi martwić się, że C++/CLI może jeszcze nie obsługiwać unique_ptr.
Układ oprogramowania jest
C# executable -> C++/CLI translation layer (dll) -> C++ dll
Więc dll C++ kompiluje grzywny za pomocą unique_ptr, ale dll C++/CLI jest w stanie prawidłowo połączyć.
Zapomniałem wspomnieć o czymś dość ważnym: Jeśli użyję unique_ptr do prostszego typu danych, na przykład do łańcucha, to z powodzeniem łączy. Na przykład:
auto string1= unique_ptr<string>(new string(20000, 'S'));
auto string2 = unique_ptr<string>(new string(20000, 'A'));
string1= std::move(string2);
i starałem się również użyć tej zmiennej, aby kompilator jej nie optymalizował.
EDIT: Właśnie testowano dodając inną funkcję zewnętrzną, która akceptuje unique_ptr<string>
i próbowałem wysyłanie w powyższym string1
i łamie, jak dobrze! Tak więc problem musi dotyczyć wygenerowanych bibliotek DLL, ponieważ funkcja std :: move() działa dobrze w każdym pliku/klasie.
Trudno jest zdiagnozować problem, gdy widzimy tylko błędy, a nie kod, który je utworzył. Powiedziałeś, że ten kod C++ działa, ale twój rzeczywisty kod C++/CLI tego nie robi. Czy możesz napisać kod C++/CLI, który wykazuje problem? –
Mogę spróbować ... Nie napisałem wiele C++/CLI od podstaw w moich czasach :) – AzP
Skomentuj wszystkie wywołania ruchu, aby uzyskać czystą kompilację. Przenieś semantykę to funkcja C++ 11 zaimplementowana przez kompilator C++. Nie przez kompilator C++/CLI. Możesz wysłać do serwisu connect.microsoft.com, aby wstrząsnąć tym drzewem. –