I want to use Code generated from the Matlab C/C++ Coder in my C++ Code.
Matlab creates the Files in the right way, and brings also a Makefile with it, to create a Library. I think, I linked the Library in the right way in my Makefile but it still throws an Error:
/HOMES/~~/testMatlab/main.cpp:11: undefined reference to `emxCreate_real_T(int, int)'
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
#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:
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. 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. But I can't change the Code generated by Matlab (libMat etc.)
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.
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. If later objects (main.o
in this case) introduce new dependencies, it doesn't go back to look for them in earlier libraries.