Problem using matlab function in visual studio c++?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Problem using matlab function in visual studio c++?

  1. #1
    Join Date
    Dec 2012
    Posts
    21

    Problem using matlab function in visual studio c++?

    I am trying to use the linprog function in visual studio 2010. I have used the matlab compiler according to the walkthrough at
    http://blogs.mathworks.com/loren/201...ries-and-dlls/.
    When I start writing the C++ document however, I cannot get things to work. The mclInitializeApplication function is undefined, and I do not know what I have to add to define it.

    Any help here would be much appreciated!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,433

    Re: Problem using matlab function in visual studio c++?

    Quote Originally Posted by gugge444 View Post
    When I start writing the C++ document however
    You are not writing a "document" -- you're writing a C++ program. A program requires you to know the programming language you're using. Using C++ isn't like using HTML, where all you need is a cheat sheet and a half-hour of free time. You have to learn the rules, syntax, nuances, etc. of the language.
    I cannot get things to work. The mclInitializeApplication function is undefined, and I do not know what I have to add to define it.
    The basic rule of C++ is that all functions that are called must be prototyped before the actual call, or the entire body of the function must appear before the call. That's how you get that function to be recognized by the compiler.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Dec 2012
    Posts
    21

    Re: Problem using matlab function in visual studio c++?

    Thank you for your reply Paul.
    Although I am new to C++ I understand the concept of prototyping. I apologize for the confusing formulation of my question. I believe this function I am trying to use is supposed to be called from an outside library of some kind, if you have a look at the page I referenced I am sure that will become clearer.

    If anyone can aid me further than to simply dismiss me as a lazy/incompetent, that would be much appreciated.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,433

    Re: Problem using matlab function in visual studio c++?

    Quote Originally Posted by gugge444 View Post
    Thank you for your reply Paul.
    Although I am new to C++ I understand the concept of prototyping. I apologize for the confusing formulation of my question. I believe this function I am trying to use is supposed to be called from an outside library of some kind, if you have a look at the page I referenced I am sure that will become clearer.
    It is the compilation that is failing. Where that function exists isn't important when compiling a module. The time where that function exists becomes important is when it's time to link, which is a separate step than compilation.
    Code:
    #include "libvigenere.h"
    #include <iostream>
    #include <cstring>
    
    int main(int ac, const char *av[])
    {
        bool enc = strcmp(av[1], "-e") == 0;
        if (!mclInitializeApplication(NULL,0))
        {
            std::cerr << "Could not initialize the application properly."
                      << std::endl;
            return -1;
        }  
    }
    It doesn't matter at this point where that function actually exists. The only thing required by the compiler is that the function is prototyped, meaning it knows the parameter types and return type. That's all -- the compiler doesn't care if that function really exists or not, whether it really is contained in another library, whether you're using MatLab or not, etc.. So take Matlab out of the loop -- your problem is compilation.

    The header file "libvigenere.h" -- what does it contain? Does it contain the prototype to the function? If not, then where is that function prototyped.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 6th, 2012 at 04:55 PM.

  5. #5
    Join Date
    Dec 2012
    Posts
    21

    Re: Problem using matlab function in visual studio c++?

    Thank you Paul!

    I needed to rethink what I was doing and am now on the right track! It is annoying however that now I have stumbled upon:

    1>C:\Program Files\MATLAB\MATLAB Compiler Runtime\v80\extern\include\mclcppclass.h(1905): fatal error C1001: An internal error has occurred in the compiler.

    This is an incredibly confusing error message as I am sure the compiler is not at fault, but at the same time it is difficult for me to try and solve this...

  6. #6
    Join Date
    Apr 1999
    Posts
    27,433

    Re: Problem using matlab function in visual studio c++?

    Quote Originally Posted by gugge444 View Post
    Thank you Paul!

    I needed to rethink what I was doing and am now on the right track! It is annoying however that now I have stumbled upon:

    1>C:\Program Files\MATLAB\MATLAB Compiler Runtime\v80\extern\include\mclcppclass.h(1905): fatal error C1001: An internal error has occurred in the compiler.
    Compiler error messages need to be accompanied by the code that doesn't compile. No one can tell you what is wrong without seeing the code.

    So please post the code, including all third-party header files and what they contain.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,970

    Re: Problem using matlab function in visual studio c++?

    Quote Originally Posted by Paul McKenzie View Post
    The basic rule of C++ is that all functions that are called must be prototyped before the actual call, or the entire body of the function must appear before the call. That's how you get that function to be recognized by the compiler.
    And that is only half the story.
    prototyping the function is what makes the C++ code compile.

    You also need to provide the linker with the necessary libraries, objects, or references to actually produce an executable.


    And when you have the above 2 sorted.
    There's still the matter of making things actually run properly, which may need you to have .dll files or other resources (COM objects, ports, mailslots, pipes) operational/accessible.

    programming is easy... honest.

  8. #8
    Join Date
    Dec 2012
    Posts
    21

    Re: Problem using matlab function in visual studio c++?

    Cheers Paul

    You are right about the fact that I do not know enough about c++ yet, as I keep getting into trouble... I have now moved onto trying to add the gnu glpk library to my code, but I keep getting unresolved external symbol errors, suggesting that I need to add some kind of library or something...

    In visual studio 2010, what I have done to try and incorporate the library is to add the additional include directory so that I can include #include <glpk.h>, but I have a feeling I need to use a linker aswell... From what I understand, I need to go to linker --> input --> additional dependencies, but when in there, all I seem to be able to do is write the name of library files... and just writing glpk_4_47.lib here does not solve the problem either.

    Once again, thank you for all your help so far!

  9. #9
    Join Date
    Dec 2012
    Posts
    21

    Re: Problem using matlab function in visual studio c++?

    Ah once again I moved on, I simply took all the files in the folder, such as dlls and libs and put them under resources, now the error that appears is that the file "glpk_4_47.dll is missing" so I wander how to change the directory under which this file is looked for

  10. #10
    Join Date
    Apr 1999
    Posts
    27,433

    Re: Problem using matlab function in visual studio c++?

    Quote Originally Posted by gugge444 View Post
    Ah once again I moved on, I simply took all the files in the folder, such as dlls and libs and put them under resources, now the error that appears is that the file "glpk_4_47.dll is missing" so I wander how to change the directory under which this file is looked for
    Are you running your app now? If you are, then the Windows OS is responsible for finding DLL's, unless somewhere your application has given a full path to the DLL (for example, if the code calls LoadLibrary( ) and gives a full path name, then the system will only use that name to load the DLL).

    If however it is just a DLL name that Windows has to load (no full path name), then Windows does a somewhat extensive but standard search for the DLL file.

    See here and look at the Remarks section:

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    So the simplest solution is to place the DLL either in a directory that is specified by your PATH environment variable, or place the DLL in the same directory as your executable file.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Dec 2012
    Posts
    21

    Re: Problem using matlab function in visual studio c++?

    I got the code to work! Thank you for all your help!

    By the way, I created a 1d array using the trick double* x = new double[var]; which seems to be the only way of creating an array with a variable size. However, I now need to use the parts of x --> (x[1], x[2], ..., x[i]) in a function that requires a double input, and I cannot do that with a double*.

    Is there a way of solving this without entering the source code and trying to change the input method, or rather, is there a way of turning a double* to a double, or better yet, create a double with a variable input?

    Thanks in advance.

  12. #12
    Join Date
    Dec 2012
    Posts
    21

    Re: Problem using matlab function in visual studio c++?

    Sorry, that is suppose to be char* and char, not double* and double

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center