开发者

Matlab / Microsoft Visual C++ 2010 Express: What is "error LNK2019: unresolved external symbol"?

开发者 https://www.devze.com 2023-03-13 20:21 出处:网络
I am trying to create a simple C++ class and a Matlab mex file.My code is as follows: Matlab: mexTest1.cpp

I am trying to create a simple C++ class and a Matlab mex file. My code is as follows:

Matlab: mexTest1.cpp

#include "mex.h"
#include "K:\\My Documents\\Visual Studio 2010\\Projects\\HelloWorld\\HelloWorld\\Class1.h"

/* Input Arguments */
#define X   prhs[0]

/* Output Arguments */
#define RESULT  plhs[0]

void mexFunction( int nlhs, mxArray *plhs[], 
          int nrhs, const mxArray *prhs[] ){ 
    /* Initialize input and output pointers
    // Inputs */
    double *x; 
    /* Outputs */
    double r;
    double *result; 

    mwSize m,n; 

    m = mxGetM(X); 
    n = mxGetN(X); 

    /* Create a matrix for the return argument */ 
    RESULT = mxCreateDoubleMatrix(1, 1, mxREAL);
    /* Assign pointers to the various parameters */ 
    result = mxGetPr(RESULT开发者_如何学Python);

    x = mxGetPr(X);

    /* Do the actual computations in a subroutine */
    Class1 c1(2, 15.0);
    r = c1.product();

    result[0] = r;
    return;

}

Class1.h:

#pragma once

#include <string> // Standard string class in C++

class Class1
{
public:
    int a;
    double b;
public:
    Class1(const int& a, const double& b);
    //virtual ~Class1();
    void print() const;
    double product() const;
};

Class1.cpp:

#include "stdafx.h"
#include <iostream>
#include "Class1.h"

Class1::Class1(const int& a, const double& b){
    Class1::a = a;
    Class1::b = b;
}

void Class1::print() const{
    std::cout << "a=" << Class1::a << " * b=" << Class1::b << " = " << Class1::product() << std::endl;
}

double Class1::product() const{
    return a*b;
}

Running the Matlab command mex mexTest1.cpp gives the error messages:

Creating library C:\DOCUME~1\K\LOCALS~1\TEMP\MEX_RH~1\templib.x and object C:\DOCUME~1\K\LOCALS~1\TEMP\MEX_RH~1\templib.exp 
mexTest1.obj : error LNK2019: unresolved external symbol "public: double __thiscall Class1::product(void)const " (?product@Class1@@QBENXZ) referenced in function _mexFunction 
mexTest1.obj : error LNK2019: unresolved external symbol "public: __thiscall Class1::Class1(int const &,double const &)" (??0Class1@@QAE@ABHABN@Z) referenced in function _mexFunction 
mexTest1.mexw32 : fatal error LNK1120: 2 unresolved externals 

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

??? Error using ==> mex at 208
Unable to complete successfully.

Can anyone help me fix this problem?

Thanks.


The linker is telling you that in trying to construct an executable, it wasn't supplied with an object file that contains Class1::product and Class1::Class1. That's because those functions would be supplied by compiling Class1.cpp, which your command line doesn't ask for.

You should use the syntax of mex for multiple files: mex mexTest1.cpp Class1.cpp


Your linker is unable to find deinitions(bodies) Class1 methods (constructor and product). This may be due to

  1. You haven't provided any definition(body)
  2. The definitions are in a lib file which you forgot to link to
0

精彩评论

暂无评论...
验证码 换一张
取 消