4
votes

I used the Matlab Coder to produce C code for a simple Matlab Array adding function which adds the elements of two arrays. Once done, the Matlab Coder gives me a package containing .c and header files ( which also includes a C file of the function itself ).

  1. How do I use these C files in for e.g. programs like Dev C++ or Code::Blocks ?

  2. How do I initialize emxArray_real_T variables to incorporate elements of an integer array ?

1
Are they C++ functions or C language functions? (C++ language functions can be overridden and overloaded, C doesn't have this capability.) - Thomas Matthews
I did that last week. Juste create an empty project from your IDE, import all the files (including the main example), compile and run. That's it! - jpo38
The files are C files. I do have Dev-C++ but whenever I try to compile the main file ( which is given by Matlab itself ), it gives me linker errors saying : " undefined reference to emxCreateND_real_T' " or " undefined reference to emxInitArray_real_T' " - Ahmad Shah
Also, when I make a new Empty Project, import all the files including the main example files, I get an error saying "mingw32-make.exe: *** No rule to make target 'Untitled3.o', needed by 'Project2.exe'. Stop." - Ahmad Shah
C is not C++ is not C! - too honest for this site

1 Answers

2
votes

If you have an IDE that supports c, like Dev C++ or Code Blocks, you just need to open the file with that IDE, compile and run it. For Code Blocks, press F9 to compile and run your code.

Edit: The undefined reference to emxInitArray_real_T error is due to an linking error. You can make your costumized MakeFile and select it in (for CodeBlocks: Project->properties->Project setting). Try this one:

CC=g++
CFLAGS=  -g
OBJECTS= main.o
LIBS = -Llibs -lMat

# --- targets
all:    main
main:   $(OBJECTS)
        $(CC)  -o main $(OBJECTS) $(LIBS)

main.o: main.cpp
        $(CC) $(CFLAGS) -Ilibs -c main.cpp

Edit 2: For Dev C++:

1 - Create a new project using File >> New Project. You can ignore the C/C++ options if you use a custom makefile. Also, an empty project will do.

2 - Add the source and header files to the new project using Project >> Add to Project or the '+' sign in the middle of the top toolbar.

3 - Go to Project >> Project Options (Alt+P) >> Makefile and tick the 'Use custom makefile' option. Then point Dev-C++ to the custom makefile below.

, as pointed in this post.