How to compile and link several c++ files with *mex*

Discussion in 'C++' started by xz, Mar 14, 2008.

  1. xz

    xz Guest

    I am programming some c++ codes to be called within Matlab.

    If there is only one c++ file, life is simple. You simply mex the cpp
    file (
    with mexFunction(...) )and call the function within Matlab.

    However, if you have several c++/h files, then what's standard
    procedure to
    compile and link the files?

    for example, I have:

    A.cpp (which contains the mexFunction and includes B.h)
    B.h (contains the declarations of some data structures used in A.cpp)
    B.cpp (implementation of the data structures declared in B.h)

    The weird thing I found is that if you simply run:

    mex B.cpp

    It just compiles without complaining there is no mexFunction() or
    main(),
    which is not like the case if you gcc some c++ file without a main()
    and
    without using the option -c.

    If you run

    mex A.cpp

    It also compiles without complaining that some function called in A
    (defined
    in B) has not be defined.


    If you run

    mex A.cpp B.cpp

    you get an A.mexa64. However, when I call this A within Matlab, it
    seems
    like the functions defined in B and called in A are ignored without
    being
    called.

    Anyway, I don't think I am compiling or linking in a correct way. "mex
    --help" does not help very much.

    Can you guys give some ideas about the compiling and linking of c++
    with mex?
    xz, Mar 14, 2008
    #1
    1. Advertising

  2. xz

    red floyd Guest

    xz wrote:
    > I am programming some c++ codes to be called within Matlab.
    >
    > I[redacted]


    Any specifics on compilation options, etc.. are OT.

    Try comp.soft-sys.matlab
    red floyd, Mar 14, 2008
    #2
    1. Advertising

  3. xz

    Guest

    On Mar 15, 12:26 am, xz <> wrote:
    > I am programming some c++ codes to be called within Matlab.
    >
    > If there is only one c++ file, life is simple. You simply mex the cpp
    > file (
    > with mexFunction(...) )and call the function within Matlab.
    >
    > However, if you have several c++/h files, then what's standard
    > procedure to
    > compile and link the files?
    >
    > for example, I have:
    >
    > A.cpp (which contains the mexFunction and includes B.h)
    > B.h (contains the declarations of some data structures used in A.cpp)
    > B.cpp (implementation of the data structures declared in B.h)
    >
    > The weird thing I found is that if you simply run:
    >
    > mex B.cpp
    >
    > It just compiles without complaining there is no mexFunction() or
    > main(),
    > which is not like the case if you gcc some c++ file without a main()
    > and
    > without using the option -c.
    >
    > If you run
    >
    > mex A.cpp
    >
    > It also compiles without complaining that some function called in A
    > (defined
    > in B) has not be defined.
    >
    > If you run
    >
    > mex A.cpp B.cpp
    >
    > you get an A.mexa64. However, when I call this A within Matlab, it
    > seems
    > like the functions defined in B and called in A are ignored without
    > being
    > called.
    >
    > Anyway, I don't think I am compiling or linking in a correct way. "mex
    > --help" does not help very much.
    >
    > Can you guys give some ideas about the compiling and linking of c++
    > with mex?


    This really is a Matlab question, not a c++ question. However, I do
    recall that the Matlab documentation covers the issue of multiple
    files being mex-ed. I've done a project before and the documentation
    there was really everything I ever needed.

    The alternative idea (and the method I'm using now) is to use a third-
    party compiler. I'm using Visual Studio, and specific instructions are
    available for it in the Matlab documentation (been doing it for a
    couple of years). I'm pretty sure its possible with gcc and the like
    as well, perhaps you should go looking around the documentation for
    more information.
    , Mar 15, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martijn van den Branden

    the trouble with mex c

    Martijn van den Branden, Jan 31, 2004, in forum: C Programming
    Replies:
    13
    Views:
    1,833
    Alan Balmer
    Feb 2, 2004
  2. Nagaraj
    Replies:
    1
    Views:
    856
    Lionel B
    Mar 1, 2007
  3. Replies:
    1
    Views:
    610
    Robert Kern
    Dec 19, 2007
  4. B.Schmidt

    mex file, nested for-loops, syntax error

    B.Schmidt, Oct 11, 2010, in forum: C Programming
    Replies:
    4
    Views:
    423
    Ben Bacarisse
    Oct 11, 2010
  5. Victor \Zverok\ Shepelev

    RDOC: several related modules in several C files

    Victor \Zverok\ Shepelev, Mar 6, 2007, in forum: Ruby
    Replies:
    3
    Views:
    182
    Max Lapshin
    Mar 16, 2007
Loading...

Share This Page