简体   繁体   English

链接Matlab生成的C ++库时出现问题

[英]Problems Linking Matlab generated C++ Library

I want to use Code generated from the Matlab C/C++ Coder in my C++ Code. 我想在我的C ++代码中使用从Matlab C / C ++编码器生成的代码。
Matlab creates the Files in the right way, and brings also a Makefile with it, to create a Library. Matlab以正确的方式创建文件,并附带一个Makefile来创建库。 I think, I linked the Library in the right way in my Makefile but it still throws an Error: 我认为,我以正确的方式在我的Makefile中链接了库,但是它仍然抛出错误:

/HOMES/~~/testMatlab/main.cpp:11: undefined reference to `emxCreate_real_T(int, int)' /HOMES/~~/testMatlab/main.cpp:11:对`emxCreate_real_T(int,int)'的未定义引用

main.cpp: main.cpp中:

1 #include<iostream>
2 #include"libMat.h"
3 
4 using namespace std;
5 
6 int main() {
7 double iHeightAll = 100;
8 double iWidthAll = 100;
9 
10 emxArray_real_T *MatlabInput;
11 MatlabInput = emxCreate_real_T((int)iHeightAll,(int)iWidthAll);
12 return 0;
13 }

libMat.h libMat.h

#ifndef __LIBMAT_H__
#define __LIBMAT_H__
/* Include files */
#include <stddef.h>
#include <stdlib.h>

#include "rtwtypes.h"
#include "libMat_types.h"

/* Function Declarations */
extern emxArray_real_T *emxCreateND_real_T(int numDimensions, int *size);
extern emxArray_real_T *emxCreateWrapperND_real_T(double *data, int numDimensions, int *size);
extern emxArray_real_T *emxCreateWrapper_real_T(double *data, int rows, int cols);
extern emxArray_real_T *emxCreate_real_T(int rows, int cols);
extern void emxDestroyArray_real_T(emxArray_real_T *emxArray);
extern double libMat(const emxArray_real_T *Pic, double height, double width);
extern void libMat_initialize();
extern void libMat_terminate();
#endif

Makefile: Makefile文件:

1 CC=g++
2 CFLAGS=  -g
3 OBJECTS= main.o
4 LIBS = -Llibs -lMat
5 
6 # --- targets
7 all:    main
8 main:   $(OBJECTS)
9         $(CC) $(LIBS)  -o main $(OBJECTS)
10         
11 main.o: main.cpp
12         $(CC) $(CFLAGS) -Ilibs -c main.cpp
13 

The Library is located in /libs and is called libMat.a. 该库位于/ libs中,称为libMat.a。 So that should all be correct 所以那应该是正确的

Do I have to call the functions in any other way because they are extern? 我是否必须以其他方式调用这些函数,因为它们是外部函数? The file libMat.h is of course implemented in libMat.cpp which is compiled in the linked Library. 当然,libMat.h文件是在链接库中编译的libMat.cpp中实现的。 But I can't change the Code generated by Matlab (libMat etc.) 但是我无法更改Matlab(libMat等)生成的代码

A problem with 32/64 Bit Stuff can probably be excluded because I build the Library on the same machine as I build my own project. 可以排除32/64 Bit Stuff的问题,因为我将库与构建自己的项目安装在同一台计算机上。

What is wrong? 怎么了?

Make it 做了

main:   $(OBJECTS)
     $(CC)  -o main $(OBJECTS) $(LIBS) # LIBS at the end

This is a funny thing with ld and static libraries: It only picks those .o files out of the .a that it thinks it needs at that stage in the linking process. 对于ld和静态库,这是一件很有趣的事情:它仅从链接过程中该阶段认为需要的.a中选择那些.o文件。 If later objects ( main.o in this case) introduce new dependencies, it doesn't go back to look for them in earlier libraries. 如果更高版本的对象(在本例中为main.o )引入了新的依赖关系,则不会在较早的库中查找它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM