2013-06-01 201 views
0

我是新鮮的c + +中,我面臨一個問題,包括C++代碼在Matlab C Mex文件。錯誤LNK2019:無法解析的外部符號

我有5個文件:RTIFederate.hRTIFederate.cppRTIFedAmb.cppRTIFedAmb.hRTI3.cppRTI3.cpp包含MEX模塊。我收到以下錯誤,同時用MEX命令和庫編譯:

Creating library C:\Users\Nudel\AppData\Local\Temp\mex_DBx_sv\templib.x and object 
    C:\Users\Nudel\AppData\Local\Temp\mex_DBx_sv\templib.exp 
RTI3.obj : error LNK2019: unresolved external symbol "class rti1516e::ObjectInstanceHandle DistributedParametersLine" ([email protected]@[email protected]@@A) referenced in function "void __cdecl mdlOutputs(struct SimStruct_tag *,int)" ([email protected]@[email protected]@[email protected]) 
RTI3.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * IEC_Model" ([email protected]@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@A) referenced in function "void __cdecl mdlOutputs(struct SimStruct_tag *,int)" ([email protected]@[email protected]@[email protected]) 
RTI3.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * IEC_Attribute" ([email protected]@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@A) referenced in function "void __cdecl mdlOutputs(struct SimStruct_tag *,int)" ([email protected]@[email protected]@[email protected]) 
RTI3.obj : error LNK2019: unresolved external symbol "class rti1516e::ObjectInstanceHandle PowerResource" ([email protected]@[email protected]@@A) referenced in function "void __cdecl mdlOutputs(struct SimStruct_tag *,int)" ([email protected]@[email protected]@[email protected]) 
RTI3.obj : error LNK2019: unresolved external symbol "class rti1516e::AttributeHandle * _classattribute" ([email protected]@[email protected]@@A) referenced in function "void __cdecl mdlOutputs(struct SimStruct_tag *,int)" ([email protected]@[email protected]@[email protected]) 
RTI3.obj : error LNK2019: unresolved external symbol "public: __thiscall RTIFedAmb::RTIFedAmb(void)" ([email protected]@[email protected]) referenced in function "public: void __thiscall RTIFederate::run(void)" ([email protected]@@QAEXXZ) 
RTI3.mexw32 : fatal error LNK1120: 6 unresolved externals 

C:\PROGRA~1\MATLAB\R2011B\BIN\MEX.PL: Error: Link of 'RTI3.mexw32' failed 

RTI3.cpp有以下一段代碼:

#include "RTIFederate.cpp" 
static void mdlOutputs(SimStruct *S, int_T tid) 
{ 
    RTIFederate *c = (RTIFederate*) ssGetPWork(S)[0]; 
    // Lê a porta de entrada correspondente 
    time_T offset = ssGetOffsetTime(S,0); 
    time_T timeOfNextHit = ssGetT(S) + offset ; 
    ssSetTNext(S, timeOfNextHit); 
    for(int_T i=0;i<NUM_INPUTS;i++) { 
     int *dims = ssGetInputPortDimensions(S, i); 
     int frameSize = dims[0]; // Tamanho do campo (se for trifasico sera 3 , por ex 
     int numChannels = dims[1]; 
     if((ssGetInputPortWidth(S,i)<1) ||(ssGetInputPortWidth(S,i)>1)|| (frameSize>1)) { 
      InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S, i); 
      real_T    *y = ssGetOutputPortRealSignal(S,i); 
      int_T    width = ssGetOutputPortWidth(S,i); 
      mexPrintf("%s%d%s%s%d\n","Num da entrada ->",i," ","Num largura > ",width);      
      for (int_T j=0;j<width;j++){ 
       *y++ =*uPtrs[j]; 
       mexPrintf("%s%u%s\n","IEC_Model[",i,"]--",IEC_Attribute[i].c_str()); 
       if(IEC_Model[i].compare("DistributedParametersLine")!=0) { 
        if(IEC_Attribute[i].compare("Voltage")) { 
         mexPrintf("%s%u%s%f\n","Voltage[",j,"]-->",*uPtrs[j]); 
         c->AtualizaValoresdeAtributos(DistributedParametersLine,_classattribute[0],*uPtrs[j],timeOfNextHit); 
        } else if(IEC_Attribute[i].compare("Current")) { 
         mexPrintf("%s%u%s%f\n","Current[",j,"]-->",*uPtrs[j]); 
         c->AtualizaValoresdeAtributos(DistributedParametersLine,_classattribute[0],*uPtrs[j],timeOfNextHit); 
        } 
       } // end if IEC_Model 
       mexPrintf("%s%f\n","Avanco de tempo1->",timeOfNextHit); 
      } // end for 
     } else { 
      double *u1=(double *) ssGetInputPortSignal(S, i); 
      real_T *y1 = ssGetOutputPortRealSignal(S,i);    
      (*y1) =(*u1); //copia entrada para saida 
      mexPrintf("%s%f\n","Avanco de tempo2->",timeOfNextHit); 
      c->AtualizaValoresdeAtributos(PowerResource,_classattribute[0],*u1,timeOfNextHit);  
     } // end else 
    } // end for i 
} 

/* Function: mdlTerminate */ 

static void mdlStart(SimStruct *S) 
{ 
    char *buf; 
    size_t buflen; 
    int status; 

    buflen = mxGetN((ssGetSFcnParam(S, 2)))*sizeof(mxChar)+1 ; // read 3rd param 
    buf = (char *)mxMalloc(buflen); //alloc mem 
    status = mxGetString((ssGetSFcnParam(S, 2)), buf,(mwSize)buflen); 

    ssGetPWork(S)[0] = (void *) new RTIFederate; // store new C++ object in the 
    RTIFederate *c = (RTIFederate *) ssGetPWork(S)[0]; 
    c->InterpretaArqMDL(buf); // Rotina que trata da interpretacao dos objetos da norma IEC 61968 
    c->run(); 
    ... 
} 

RTIFederate.cpp我已經聲明頂部以下內容:

#include "RTIFedAmb.h" 
#include "RTIFederate.h" 

and in the file RTIFederate.h我聲明:

class RTIFederate 
{ 
public: 
    RTIambassador *rtiamb; 
    RTIFedAmb  *fedamb; 

    // variables // 

    ObjectClassHandle _ClassObject[300]; 
    AttributeHandle _classattribute[300]; 
    string IEC_Model[29],IEC_Attribute[20];// 

    // public methods // 

    RTIFederate(); 
    virtual ~RTIFederate(); 
    ... 
} 

extern ObjectClassHandle _ClassObject[300]; 
extern AttributeHandle _classattribute[300]; 
extern AttributeHandleSet attributeSet[300]; 

extern ObjectInstanceHandle ProtectedSwitch,Recloser,ThreePhaseBreaker,ACLineSegment,DistributedParametersLine; 

extern RTIambassador *rtiamb; 
extern RTIFedAmb  *fedamb; 

也有是在RTIFedAmb.h:

//methods 
RTIFedAmb(); 
virtual ~RTIFedAmb() throw(); 

一段代碼誰能幫我解釋一下我失去了什麼?

回答

1

所以,你想在你的C++項目中包含c-lib。這很好,但你應該注意一件事:C++與c有點不同。你找到了一個很好的例子。 C++「變形」lib中的函數名稱,而c沒有。儘管如此,你仍然可以使用C庫。這樣做:

 extern "C" ObjectClassHandle _ClassObject[300]; 
    extern "C" AttributeHandle _classattribute[300]; 
    extern "C" AttributeHandleSet attributeSet[300]; 

    extern "C" ObjectInstanceHandle ProtectedSwitch,Recloser,ThreePhaseBreaker,ACLineSegment,DistributedParametersLine; 

    extern "C" RTIambassador *rtiamb; 
    extern "C" RTIFedAmb  *fedamb; 

希望它能幫助你。

+0

有趣。我正在嘗試另一件事。因爲我有一個RTIFederate指針(c),我改變了行c-> AtualizaValoresdeAtributos(DistributedParametersLine,_classattribute [0],* uPtrs [j],timeOfNextHit); to c-> AtualizaValoresdeAtributos(c-> DistributedParametersLine,c - > _ classattribute [0],* uPtrs [j],timeOfNextHit); – Andre

+0

並編譯時沒有以前的錯誤。但是我仍然有一個LNK2019的鏈接問題:在函數「public:void __thiscall RTIFederate :: run(void)」中引用了未解析的外部符號「public:__thiscall RTIFedAmb :: RTIFedAmb(void)」(?? 0RTIFedAmb @@ QAE @ XZ) 「(?run @ RTIFederate @@ QAEXXZ),我能找到答案。任何想法 ? – Andre

+0

順便說一句,我改爲extern「C」,但我仍然得到相同的錯誤。當我使用c-> DistributedParametersLine時,編譯就OK了。 – Andre

相關問題