[Gdal-dev] Trouble linking GDAL library and Matlab

Gennady Khokhorin gok at aerometric-ak.com
Fri May 4 19:07:05 EDT 2007


Guillaume,

I did switch to cpp and was able to create .mexs32 file as well as .dll dynamically linked to gdal.
Steps you could try:
1. rename file to test.cpp;
2. load libs in the header:
#include "mex.h"
#include "C:\my_Projects\packages\gdal-1.4.0\distrib\include\gdal_priv.h"
#pragma comment (lib,"gdal_i-vc8.lib") // dynamic link
#pragma comment (lib,"libmex.lib") // static link (for mexErrMsgTxt)
3. copy both libs to test.cpp folder;
4. matlab: "mex test.cpp" or "mcc -v -B cpplib:testlib test.cpp"

My environment: msvc8.0+gdal1.4.0

Happy programming!
gok

-----Original Message-----
From: gdal-dev-bounces at lists.maptools.org [mailto:gdal-dev-bounces at lists.maptools.org]On Behalf Of guillaume huby
Sent: Friday, May 04, 2007 1:13 AM
To: gdal-dev
Subject: [Gdal-dev] Trouble linking GDAL library and Matlab


Hello,

Matlab offers the possibility to compile C programs and use them as matlab functions. This is called Mex files and works almost like making a dll. I used Visual Studio 2005 Pro and FWTools 1.2.1 in order to be able to use GDAL inside Matlab. 

The simple C program I made is below and just take a string in input and returns 1.

---------------------------------Start Code-----------------------------------------
#include "gdal_priv.h"
#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{

    double *y;
    char *pszFilename;
    int bufLen;
    int status; 

    /* Create a 1-by-1 matrix for the return argument. */
    plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);

    /* Input must be a string. */
    if (mxIsChar(prhs[0]) != 1)
        mexErrMsgTxt("Input must be a string."); 

    /* Input must be a row vector. */
    if (mxGetM(prhs[0]) != 1)
        mexErrMsgTxt("Input must be a row vector");

    /* Get the length of the input string. */
    bufLen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1; 

    /* Allocate memory for input string. */
    pszFilename = (char *) mxCalloc(bufLen, sizeof(char));

    /* Copy string data from prhs[0] into a C string inputBuf. */
    status = mxGetString(prhs[0], pszFilename, bufLen); 
    if (status != 0)
        mexWarnMsgTxt("Not enough space. String is truncated.");

    /*GDALDataset  *poDataset;

    GDALAllRegister();*/

    /*poDataset = (GDALDataset *) GDALOpen( pszFilename, GA_ReadOnly ); 
    if( poDataset == NULL ) {
    } else {
        mexWarnMsgTxt("Could not open dataset.");
    }*/

    /* Assign a pointer to the output. */
    y = mxGetPr(plhs[0]);

    *y = 1.0 ;
}
--------------------------------------------End Code-------------------------------------

Without #include "gdal_priv.h" everything is fine, but with there is a problem of dependency (Matlab cannot find some library) althought all gdal (all found in FWTools\bin) dlls are in path (checked with depends walker). 
Discussion with Matlab support identified the problem in "gdal_priv.h" line 56 #include <vector>. AFAIK, <vector> is a STL standard C++ library.
Did I missed something ? linking with some special library ? 

Thanks for your help.
Guillaume.




More information about the Gdal-dev mailing list