title: error LNK2019: unresolved external symbol
body: <p>I'm fresh in c++ and I'm facing a problem in including C++ code in Matlab C Mex-file.</p>
  
  <p>I have 5 files: <code>RTIFederate.h</code>, <code>RTIFederate.cpp</code>, <code>RTIFedAmb.cpp</code>, <code>RTIFedAmb.h</code>, <code>RTI3.cpp</code>. <code>RTI3.cpp</code> contains the MEX modules. I'm getting the following errors while compiling with MEX command and libraries:</p>
  
  <pre><code>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&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; * 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&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; * 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
  </code></pre>
  
  <p>My <code>RTI3.cpp</code> has the following piece of code:</p>
  
  <pre><code>#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&lt;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)&lt;1) ||(ssGetInputPortWidth(S,i)&gt;1)|| (frameSize&gt;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 -&gt;",i," ","Num largura &gt; ",width);                      
              for (int_T j=0;j&lt;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,"]--&gt;",*uPtrs[j]);
                          c-&gt;AtualizaValoresdeAtributos(DistributedParametersLine,_classattribute[0],*uPtrs[j],timeOfNextHit);
                      } else if(IEC_Attribute[i].compare("Current")) {
                          mexPrintf("%s%u%s%f\n","Current[",j,"]--&gt;",*uPtrs[j]);
                          c-&gt;AtualizaValoresdeAtributos(DistributedParametersLine,_classattribute[0],*uPtrs[j],timeOfNextHit);
                      } 
                  } // end if IEC_Model
                  mexPrintf("%s%f\n","Avanco de tempo1-&gt;",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-&gt;",timeOfNextHit); 
              c-&gt;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-&gt;InterpretaArqMDL(buf);  // Rotina que trata da interpretacao dos objetos da norma IEC 61968
      c-&gt;run(); 
      ...
  }
  </code></pre>
  
  <p>On top of <code>RTIFederate.cpp</code> I have declared the following:</p>
  
  <pre><code>#include "RTIFedAmb.h"
  #include "RTIFederate.h"
  </code></pre>
  
  <p>and in the file <code>RTIFederate.h</code> I declared:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>Also there is a piece of code in <code>RTIFedAmb.h:</code></p>
  
  <pre><code>//methods
  RTIFedAmb();
  virtual ~RTIFedAmb() throw();
  </code></pre>
  
  <p>Can anyone help me to explain what am I missing ?</p>