错误 LNK2019:未解析的外部符号

Posted

技术标签:

【中文标题】错误 LNK2019:未解析的外部符号【英文标题】:error LNK2019: unresolved external symbol 【发布时间】:2013-06-01 12:10:40 【问题描述】:

我是 C++ 的新手,在 Matlab C Mex 文件中包含 C++ 代码时遇到了问题。

我有 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" (?DistributedParametersLine@@3VObjectInstanceHandle@rti1516e@@A) referenced in function "void __cdecl mdlOutputs(struct SimStruct_tag *,int)" (?mdlOutputs@@YAXPAUSimStruct_tag@@H@Z) 
RTI3.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * IEC_Model" (?IEC_Model@@3PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) referenced in function "void __cdecl mdlOutputs(struct SimStruct_tag *,int)" (?mdlOutputs@@YAXPAUSimStruct_tag@@H@Z) 
RTI3.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * IEC_Attribute" (?IEC_Attribute@@3PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) referenced in function "void __cdecl mdlOutputs(struct SimStruct_tag *,int)" (?mdlOutputs@@YAXPAUSimStruct_tag@@H@Z) 
RTI3.obj : error LNK2019: unresolved external symbol "class rti1516e::ObjectInstanceHandle PowerResource" (?PowerResource@@3VObjectInstanceHandle@rti1516e@@A) referenced in function "void __cdecl mdlOutputs(struct SimStruct_tag *,int)" (?mdlOutputs@@YAXPAUSimStruct_tag@@H@Z) 
RTI3.obj : error LNK2019: unresolved external symbol "class rti1516e::AttributeHandle * _classattribute" (?_classattribute@@3PAVAttributeHandle@rti1516e@@A) referenced in function "void __cdecl mdlOutputs(struct SimStruct_tag *,int)" (?mdlOutputs@@YAXPAUSimStruct_tag@@H@Z) 
RTI3.obj : error LNK2019: unresolved external symbol "public: __thiscall RTIFedAmb::RTIFedAmb(void)" (??0RTIFedAmb@@QAE@XZ) referenced in function "public: void __thiscall RTIFederate::run(void)" (?run@RTIFederate@@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"

在文件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;

希望对你有所帮助。

【讨论】:

有趣。我在尝试另一件事。因为我有一个指向 RTIFederate 的指针 (c),所以我更改了行 c->AtualizaValoresdeAtributes(DistributedParametersLine,_classattribute[0],*uPtrs[j],timeOfNextHit); to c->AtualizaValoresdeAtributos(c->DistributedParametersLine,c->_classattribute[0],*uPtrs[j],timeOfNextHit); 它编译时没有出现以前的错误。但我仍然有 LNK2019 的链接问题:未解析的外部符号“public: __thiscall RTIFedAmb::RTIFedAmb(void)”(??0RTIFedAmb@@QAE@XZ) 在函数“public: void __thiscall RTIFederate::run(void)”中引用" (?run@RTIFederate@@QAEXXZ) 我能够找到答案。有什么想法吗? 顺便说一句,我改为 extern "C" 但我仍然遇到同样的错误。当我使用 c->DistributedParametersLine 时,编译正常。

以上是关于错误 LNK2019:未解析的外部符号的主要内容,如果未能解决你的问题,请参考以下文章

错误 LNK2019:未解析的外部符号 opencv

错误 LNK2019:未解析的外部符号

LNK2019“未解析的外部符号”错误(C++ OpenGL)

opencv/visual studio:错误 LNK2019:未解析的外部符号

错误 LNK2019:Visual Studio 中未解析的外部符号 [重复]

错误 LNK2019:matlab 中未解析的外部符号