Matlab in C :: linking problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Matlab in C :: linking problem

  1. #1
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Matlab in C :: linking problem

    I am using DevC++ as compiler on Windows 2000.

    I tried the code from Matlab's documentation to use matlab functions in C, but its giving an compile time error.
    Code:
      [Linker error] undefined reference to `engOpen' 
      // same for other matlab declarations and function calls
    I have added the lib and include directories with matlab into the compiler options.

    this is the code
    Code:
    /*
     *  engdemo.c
     *
     *  This is a simple program that illustrates how to call the
     *  MATLAB engine functions from a C program.
     *
     * Copyright (c) 1984-2000 The MathWorks, Inc.
     * Revision: 1.8 $
     */
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include "engine.h"
    #define  BUFSIZE 256
    
    int main()
    
    {
       Engine *ep;
       mxArray *T = NULL, *result = NULL;
       char buffer[BUFSIZE];
       double time[10] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,
                           8.0, 9.0};
       /*
        * Start the MATLAB engine locally by executing the string
        * "matlab".
        *
        * To start the session on a remote host, use the name of
        * the host as the string rather than \0.
        *
        * For more complicated cases, use any string with whitespace,
        * and that string will be executed literally to start MATLAB.
        */
       if (!(ep = engOpen("\0"))) {
          fprintf(stderr, "\nCan't start MATLAB engine\n");
          return EXIT_FAILURE;
       }
    
       /*
        * PART I
        *
        * For the first half of this demonstration, we will send data
        * to MATLAB, analyze the data, and plot the result.
        */
    
       /* 
        * Create a variable for our data.
        */
       T = mxCreateDoubleMatrix(1, 10, mxREAL);
       memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));
    
       /*
        * Place the variable T into the MATLAB workspace.
        */
       engPutVariable(ep, "T", T);
    
       /*
        * Evaluate a function of time, distance = (1/2)g.*t.^2
        * (g is the acceleration due to gravity).
        */
       engEvalString(ep, "D = .5.*(-9.8).*T.^2;");
    
       /*
        * Plot the result.
        */
       engEvalString(ep, "plot(T,D);");
       engEvalString(ep, "title('Position vs. Time for a falling object');");
       engEvalString(ep, "xlabel('Time (seconds)');");
       engEvalString(ep, "ylabel('Position (meters)');");
    
       /*
        * Use fgetc() to make sure that we pause long enough to be
        * able to see the plot.
        */
       printf("Hit return to continue\n\n");
       fgetc(stdin);
    
       /*
        * We're done for Part I! Free memory, close MATLAB engine.
        */
       printf("Done for Part I.\n");
       mxDestroyArray(T);
       engEvalString(ep, "close;");
    
       /*
        * PART II
        *
        * For the second half of this demonstration, we will request
        * a MATLAB string, which should define a variable X.  MATLAB
        * will evaluate the string and create the variable.  We
        * will then recover the variable, and determine its type.
        */
         
       /*
        * Use engOutputBuffer to capture MATLAB output, so we can
        * echo it back.
        */
    
       engOutputBuffer(ep, buffer, BUFSIZE);
       while (result == NULL) {
          char str[BUFSIZE];
    
          /*
           * Get a string input from the user.
           */
          printf("Enter a MATLAB command to evaluate.  This command should\n");
          printf("create a variable X.  This program will then determine\n");
          printf("what kind of variable you created.\n");
          printf("For example: X = 1:5\n");
          printf(">> ");
    
          fgets(str, BUFSIZE-1, stdin);
          
          /*
           * Evaluate input with engEvalString.
           */
          engEvalString(ep, str);
            
          /*
           * Echo the output from the command.  First two characters
           * are always the double prompt (>>).
           */
          printf("%s", buffer+2);
    
          /*
           * Get result of computation.
           */
          printf("\nRetrieving X...\n");
          if ((result = engGetVariable(ep,"X")) == NULL)
             printf("Oops! You didn't create a variable X.\n\n");
          else {
             printf("X is class %s\t\n", mxGetClassName(result));
          }
       }
    
       /*
        * We're done! Free memory, close MATLAB engine and exit.
        */
       printf("Done!\n");
       mxDestroyArray(result);
       engClose(ep);
       
       return EXIT_SUCCESS;
    }

  2. #2
    Join Date
    Feb 2009
    Location
    Ukraine
    Posts
    64

    Re: Matlab in C :: linking problem

    Well, you added the directories to the compiler options, now try to add corresponding matlab libraries to the linker options.
    There must be a string when you enumerate libs which your program will be linked with. I mean *.lib and *.obj files.

  3. #3
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Resolved Re: Matlab in C :: linking problem

    Well thanks I finally got it to work; but creating a adding the libraries in the project options through DevC++ IDE.
    Last edited by aniskhan; February 20th, 2009 at 06:49 AM.

  4. #4
    Join Date
    Jul 2013
    Posts
    1

    Re: Matlab in C :: linking problem

    Quote Originally Posted by aniskhan View Post
    Well thanks I finally got it to work; but creating a adding the libraries in the project options through DevC++ IDE.

    Hello,

    I have the same problem with calling Matlab from C,
    I am using DevC++ and Matlab 2011a, I added the libraries in the project options but I still have the same errors:

    Errors:
    [Linker error] undefined reference to `mxSetName'
    [Linker error] undefined reference to `mxGetPr'
    ......

    [Linker error] undefined reference to `engClose'

    Can you please give me more details on how did you manage this problem

    Thank you,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center