初学者应对八度音阶和 mex 问题

Beginner grappling with octave and mex issues

我是一位经验丰富的 C/C++ 开发人员,但在处理 Octave 中的 mex 时我非常新手。我确定我在这里遗漏了一些基本的东西,但我找不到它是什么。

这些是我的文件:

myhello.cpp   
test.cpp   
test.h

这是文件的内容

(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;
}

然后我通过 ./运行-octave --no-gui 启动 Octave 4.0.0,并在提示符下键入以下内容:

mex -v myhello.cpp test.cpp

我得到的回复是:

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

我再次收到提示。

我输入

myhello(1,2,3)

得到这个:

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

所以很明显有些东西没有正确链接,但我不知道如何让一切都这样做。抱歉,我已经搜索了一段时间,但没有找到任何可以解决这个简单问题的方法。

提前致谢, 本

P.S。我的系统是 Ubuntu 15.04,64 位。

我什至无法在 VS2013 中编译(link,实际上),除非我在 namespace test 中定义 void testMethod()。这有效:

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

using namespace std;

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

没有namespace test{ ... },有未解析的符号:

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

当它工作时,我得到:

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