2014-10-06 54 views
0

我想通過Matlab Engine製作一個與Matlab接口的C程序,它也通過Glut使用OpenGL。我已經成功地編譯並運行了C程序來完成這些工作(Matlab Engine或Glut),但是我在編譯使用這兩個程序的程序時遇到了麻煩。鏈接頭文件:Matlab引擎和OpenGL

特別是,我使用以下命令與gcc:gcc -o test test.c -I/Applications/MATLAB_R2014a.app/extern/include/ -framework GLUT -framework OpenGL。 -I標誌是告訴鏈接到engine.h和matrix.h頭文件所在的目錄。編譯器抱怨Matlab引擎和矩陣庫函數是未定義的符號:

Undefined symbols for architecture x86_64: 
    "_engEvalString", referenced from: 
     _main in test-bae966.o 
    "_engGetVariable", referenced from: 
     _main in test-bae966.o 
    "_engOpen", referenced from: 
     _main in test-bae966.o 
    "_engPutVariable", referenced from: 
     _main in test-bae966.o 
    "_mxCreateDoubleScalar", referenced from: 
     _main in test-bae966.o 
    "_mxGetPr", referenced from: 
     _main in test-bae966.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

這裏是我試圖編譯的test.c文件。我現在不需要特別做任何事情。首先,我只想看看我是否可以使用Matlab Engine和OpenGL編寫一個C程序。

#include <GLUT/glut.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <engine.h> 
#include <matrix.h> 

void display(void) 
{ 
    /* clear all pixels */ 
    glClear(GL_COLOR_BUFFER_BIT); 
    /* draw white polygon (rectangle) with corners at 
    * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) 
    */ 
    glColor3f(1.0, 1.0, 1.0); 
    glBegin(GL_POLYGON); 
    glVertex3f(0.25, 0.25, 0.0); 
    glVertex3f(0.75, 0.25, 0.0); 
    glVertex3f(0.75, 0.75, 0.0); 
    glVertex3f(0.25, 0.75, 0.0); 
    glEnd(); 
    /* don’t wait! 
    * start processing buffered OpenGL routines 
    */ 
    glFlush(); 
} 

void init(void) 
{ 
    /* select clearing (background) color */ 
    glClearColor(0.0, 0.0, 0.0, 0.0); 
    /* initialize viewing values */ 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); 
} 

/* 
* Declare initial window size, position, and display mode 
* (single buffer and RGBA). Open window with 「hello」 
* in its title bar. Call initialization routines. 
* Register callback function to display graphics. 
* Enter main loop and process events. 
*/ 
int main(int argc, char** argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(250, 250); 
    glutInitWindowPosition(100, 100); 
    glutCreateWindow("hello"); 
    init(); 

    Engine *ep; 
    mxArray *pa = NULL, *res = NULL; 

    if (!(ep = engOpen(""))) { 
      fprintf(stderr, "\nCan't start MATLAB engine\n"); 
      return EXIT_FAILURE; 
     } 

    pa = mxCreateDoubleScalar(5); 
    engPutVariable(ep, "a", pa); 
    engEvalString(ep, "res = 2 * a"); 
    res = engGetVariable(ep,"res"); 
    int resVal = *mxGetPr(res); 
    printf("%d\n", resVal); 

    glutDisplayFunc(display); 
    glutMainLoop(); 
    return 0; /* ISO C requires main to return int. */ 
} 

回答

0

您有鏈接錯誤。你需要告訴gcc你的程序試圖調用的包含MATLAB函數的文件的名稱和位置。您可以添加指定目錄的-L選項,然後添加指定文件的-l選項。

例如,如果所需的庫是/Applications/MATLAB_R2014a.app/extern/lib/libengine.dylib,那麼您將在編譯命令中添加-L/Applications/MATLAB_R2014a.app/extern/lib -lengine

這類事情很快就會變老,所以通常會寫一個腳本 - 或者更好的Makefile--所以你不必每次都重新輸入所有這些亂七八糟的東西。

+0

謝謝,這工作。我需要的庫是libeng.dylib和libmx.dylib,位於/Applications/MATLAB_R2014a.app/bin/maci64。我必須使用-leng和-lmx標誌鏈接到它們,因爲我認爲gcc會自動將'lib'放在庫的前面並尋找相關的擴展名(例如eng - > libeng.dylib)。總之,這是我編譯上述代碼的方法:'gcc -o test test.c -I/Applications/MATLAB_R2014a.app/extern/include/-L/Applications/MATLAB_R2014a.app/bin/maci64 -leng -lmx - 框架GLUT -framework OpenGL' – 2014-10-07 13:46:24