2012-09-19 11 views
7

में unique_ptr का उपयोग करते समय लिंकर त्रुटि मैं वर्तमान में auto_ptr से unique_ptr पर अपने उदाहरणों को परिवर्तित कर रहा हूं, लेकिन मैं कोई समस्या मार रहा हूं। यह कोड के सी ++ भाग में बहुत अच्छा काम करता है, लेकिन जब इसे मेरे प्रबंधित सी ++/सीएलआई परत में किया जाता है (सॉफ़्टवेयर सी # और सी ++ दोनों का उपयोग करता है) मुझे लिंक त्रुटियां मिलती हैं। यह ठीक संकलित करता है, लेकिन यह लिंक समय पर टूट जाता है। auto_ptr के साथ कभी भी कोई समस्या नहीं थी।C++/CLI

मैं वर्तमान में विजुअल स्टूडियो 2010 का उपयोग कर रहा हूं। क्या किसी को सी ++/सीएलआई में unique_ptr का उपयोग करने के साथ किसी भी समस्या का पता है?

मैं नीचे दिए गए कोड का एक टुकड़ा से मेरी समस्या का योग करने के लिए, लेकिन कृपया ध्यान रखें कि नीचे कोड वास्तव में संकलित करता है तथा काम करता है की कोशिश की है (मैं जाँच की है कि संकेत के स्वामित्व ठीक से ले जाया गया है)। मुझे संकलन करते समय त्रुटियों को जोड़ने नहीं मिल रहा है, लेकिन नीचे दिया गया कोड शुद्ध सी ++ है और सी ++/सीएलआई नहीं है। मैं बस एक छोटा सा उदाहरण चाहता था कि कोड कैसे बनाया गया है ताकि लिंकर त्रुटि पढ़ने के लिए और अधिक समझ में आती है।

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]?$a[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 

मुझे करता है कि जैसा कि आप देख सकते हैं (यदि आप अविश्वसनीय रूप से भयानक संदेशों के माध्यम से प्राप्त कर सकते हैं) MarshalCopy को कुछ संदर्भों देखते हैं, कुछ:

#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 

त्रुटि मैं जब जोड़ने मिल पीछा कर रहा है चिंता करें कि सी ++/सीएलआई अभी तक unique_ptr का समर्थन नहीं कर सकता है।

सॉफ्टवेयर के लेआउट

C# executable -> C++/CLI translation layer (dll) -> C++ dll 

तो सी ++ dll unique_ptr का उपयोग कर ठीक संकलित है, लेकिन C++/CLI dll ठीक से लिंक करने में असमर्थ है।

मैं कुछ महत्वपूर्ण उल्लेख करने के लिए भूल गया: अगर मैं एक सरल डेटाटाइप के लिए unique_ptr का उपयोग करता हूं, उदाहरण के लिए एक स्ट्रिंग, यह सफलतापूर्वक लिंक करता है। उदाहरण के लिए:

auto string1= unique_ptr<string>(new string(20000, 'S')); 
    auto string2 = unique_ptr<string>(new string(20000, 'A')); 
    string1= std::move(string2); 

और मैंने चर का उपयोग करने के लिए भी सुनिश्चित करने की कोशिश की है ताकि संकलक इसे अनुकूलित नहीं कर सके।

संपादित करें: मैं सिर्फ एक और बाहरी समारोह है कि एक unique_ptr<string> स्वीकार करता जोड़ने परीक्षण किया है, और मैं ऊपर string1 में भेजने की कोशिश की और कहा कि साथ ही टूट जाता है! इसलिए समस्या जेनरेट डीएलएल के बीच होनी चाहिए, क्योंकि std :: move() प्रत्येक फ़ाइल/कक्षा के भीतर अच्छी तरह से काम करता है।

+0

किसी समस्या का निदान करना मुश्किल है जब हम केवल त्रुटियों को देखते हैं, न कि उन्हें बनाए गए कोड। आपने कहा कि यह सी ++ कोड काम करता है, लेकिन आपका वास्तविक सी ++/सीएलआई कोड नहीं है। क्या आप कुछ सी ++/सीएलआई कोड लिख सकते हैं जो समस्या को प्रदर्शित करता है? –

+0

मैं कोशिश कर सकता हूं ... मेरे दिनों में स्क्रैच से बहुत सी ++/सीएलआई नहीं लिखा है :) – AzP

+0

एक साफ संकलन प्राप्त करने के लिए सभी चाल कॉल को टिप्पणी करें। Semantics ले जाएँ C++ कंपाइलर द्वारा लागू एक C++ 11 सुविधा है। सी ++/सीएलआई कंपाइलर द्वारा नहीं। आप उस पेड़ को हिलाकर कनेक्ट करने के लिए connect.microsoft.com पर पोस्ट कर सकते हैं। –

उत्तर

8

अच्छा, अब यह नहीं पता कि यह कितना प्रासंगिक है, लेकिन मुझे सी ++/सीएलआई के साथ बिल्कुल समस्या थी और इसे मूल्य के बजाय आर-वैल्यू संदर्भ लेने के साथ हल किया गया। मेरा मतलब है: शून्य setUniquePtr (unique_ptr & & एक, unique_ptr & & ख)

इस तरह यह संकलित हालांकि यह काम करने के लिए एक स्पष्ट तरीका नहीं है।

+0

इसे उत्तर के रूप में चिह्नित किया जाना चाहिए। मेरे लिए काम किया। – AarCee

2

उस जटिल त्रुटि संदेश के माध्यम से पढ़ना, मुझे लगता है कि यह शिकायत कर रहा है कि LinAlgPoint3 संरचना में एक कॉपी कन्स्ट्रक्टर नहीं है। कॉपी कन्स्ट्रक्टर को लागू करने का प्रयास करें, और शायद ऑपरेटर = और ==, और देखें कि यह ठीक करता है या नहीं।

+0

एक अच्छे सुझाव के लिए धन्यवाद। आप सही थे, कक्षा में एक असाइनमेंट-ऑपरेटर के साथ-साथ कॉपी कन्स्ट्रक्टर (इसमें पहले से ही '==') था, लेकिन उन्हें जोड़ने से समस्या ठीक नहीं हुई .. :( – AzP

+0

मैंने अभी कुछ और जानकारी जोड़ दी है प्रश्न का अंत – AzP

1

आप डीएलएल के बीच सी ++ ऑब्जेक्ट्स पास नहीं कर सकते हैं और उम्मीद कर सकते हैं कि यह सही काम करे।

मेमोरी लेआउट विभिन्न मॉड्यूल में अलग हो सकता है। विभिन्न आवंटकों का लगभग निश्चित रूप से उपयोग किया जा रहा है (यह string, vector, और unique_ptr सहित मेमोरी का मालिक है, जो किसी भी सी ++ प्रकार को शूट करता है)।

शुद्ध आभासी आधार वर्गों के पॉइंटर्स का उपयोग इससे मदद कर सकता है।

या, यदि आपके पास C++ भाग के लिए स्रोत कोड है, तो एक अलग DLL में C++ कोड को संदर्भित करने के बजाय C++ और C++/CLI को एक एकल DLL में जोड़ने का प्रयास करें।

+0

लेकिन अनन्य_प्टर विनाशक को एक अंतर्निहित सूचक लेता है जो कि सही प्रकार के आधार पर सही कॉल करने में सक्षम होता है। – AzP

+0

@AzP: यह केवल विनाशक को सूचक नहीं लेता है, यह प्रत्येक डिलीटर के लिए विशिष्ट है। मॉड्यूल सीमाओं में समस्याएं पैदा करता है। –

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^