2014-10-06 3 views
0

Matlab Engine을 통해 Matlab과 인터페이스하고 Glut을 통해 OpenGL을 사용하는 C 프로그램을 만들려고합니다. 성공적으로 컴파일하고 C 프로그램을 실행합니다 (Matlab Engine 또는 Glut). 그러나이 두 가지를 사용하는 프로그램을 컴파일하는 데 문제가 있습니다.헤더 파일 링크하기 : Matlab Engine 및 OpenGL

특히 gcc : gcc -o test test.c -I/Applications/MATLAB_R2014a.app/extern/include/ -framework GLUT -framework OpenGL과 함께 다음 명령을 사용하고 있습니다. -I 플래그는 engine.h 및 matrix.h 헤더 파일이있는 디렉토리에 링크를 알리는 것입니다.

다음
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에 엔진 및 매트릭스 라이브러리 함수가 정의되지 않은 상징 뿌려줍니다. 지금 당장은 아무것도 할 필요가 없습니다. 먼저, C 프로그램이 Matlab Engine과 OpenGL을 둘 다 사용할 수 있는지보고 싶습니다.

#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

링커 오류가 있습니다. 프로그램이 호출하려고하는 MATLAB 함수가 포함 된 파일의 이름과 위치를 gcc에 알려줘야합니다. 디렉토리를 지정하는 -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에 있습니다. gcc가 자동으로 라이브러리 앞에 'lib'를 넣고 관련 확장 (예 : eng -> libeng.dylib)을 찾았 기 때문에 -leng 및 -lmx 플래그로 링크해야했습니다. 위의 코드를 요약하면 다음과 같습니다 :'gcc -o test test.c -I/Applications/MATLAB_R2014a.app/extern/include/-L/Applications/MATLAB_R2014a.app/bin/maci64 -leng -lmx - 프레임 워크 GLUT -framework OpenGL' –