我试图制作一个通过Matlab Engine与Matlab接口的C程序,并通过Glut使用OpenGL。我已成功编译并运行执行其中一项操作的C程序(Matlab Engine OR Glut),但我在编译同时使用这两种程序的程序时遇到了问题。
特别是,我在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 :(得分:0)
您有链接器错误。您需要告诉gcc包含程序试图调用的MATLAB函数的文件的名称和位置。您可以添加指定目录的-L选项,然后添加指定文件的-l选项。
例如,如果所需的库是/Applications/MATLAB_R2014a.app/extern/lib/libengine.dylib,那么您可以将-L/Applications/MATLAB_R2014a.app/extern/lib -lengine
添加到编译命令中。
这种事情很快就会变老,所以人们通常会编写一个脚本 - 或者更好的是一个Makefile - 所以你不必每次都重新输入所有这些混乱。