1
votes

I'm an experienced C/C++ developer, but I'm extremely green when it comes to dealing with mex in octave. I'm sure I'm missing something basic here, but I can't find what it is.

These are my files:

myhello.cpp   
test.cpp   
test.h

Here are the contents of the files

(myhello.cpp):

#include "test.h"
#include "mex.h"

using namespace test;

void
mexFunction (int nlhs, mxArray *plhs[],
             int nrhs, const mxArray *prhs[])
{
  mexPrintf ("Hello, World!\n");
  testMethod();

  mexPrintf ("I have %d inputs and %d outputs\n", nrhs, nlhs);
}

(test.h)

namespace test
{
        void testMethod();
}

(test.cpp)

#include "test.h"
#include <iostream>

using namespace std;
using namespace test;

void testMethod()
{
        cout << "this works." << endl;
}

So then I launch Octave 4.0.0 through ./run-octave --no-gui, and type the following at the prompt:

mex -v myhello.cpp test.cpp

The response I get is:

g++ -c -fPIC -I/usr/local/include/octave-4.0.0/octave/.. -I/usr/local/include/octave-4.0.0/octave -I/usr/local/include -pthread -fopenmp -g -O2 -I. myhello.cpp -o myhello.o g++ -c -fPIC -I/usr/local/include/octave-4.0.0/octave/.. -I/usr/local/include/octave-4.0.0/octave -I/usr/local/include -pthread -fopenmp -g -O2 -I. test.cpp -o test.o g++ -shared -Wl,-Bsymbolic -o myhello.mex myhello.o test.o -L/usr/local/lib/octave/4.0.0 -L/usr/local/lib -loctinterp -loctave

And I'm once again presented with the prompt.

I type

myhello(1,2,3)

And get this:

error: /home/brush/Documents/mex_tests/myhello.mex: failed to load: /home/brush/Documents/mex_tests/myhello.mex: undefined symbol: _ZN4test10testMethodEv

So obviously something isn't linking properly, but I cannot figure out how to get everything to do so. Sorry, but I've searched for a while and haven't found anything that fixed this simple issue.

Thanks in advance, Ben

P.S. My system is Ubuntu 15.04, 64-bit.

1
Try with -v to see if there are any more clues. Also, this is a long shot, but try putting #include "mex.h" first. It's like it thinks testMethod is extern... or dynamically loaded. - chappjc
Wait, shouldn't void testMethod() be defined in test.CPP in namespace test? - chappjc
@chappjc I was getting a number of foreign, weird errors in the code (I usually work in Microsoft Visual Studio), but to get mex to compile I had to include the namespace at the start of the test.cpp file and then define the function after. - user2415010

1 Answers

1
votes

I can't even compile (link, actually) in VS2013 unless I define void testMethod() in namespace test. This works:

//test.cpp
#include "test.h"
#include <iostream>

using namespace std;

namespace test
{
    void testMethod()
    {
            cout << "this works." << endl;
    }
}

Without namespace test{ ... }, there are unresolved symbols:

myhello.obj : error LNK2019: unresolved external symbol "void __cdecl test::testMethod(void)"

When it works, I get:

>> myhello
Hello, World!
I have 0 inputs and 0 outputs