4

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 ?

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
Ahmad Shah
  • 199
  • 4
  • 14
  • Are they C++ functions or C language functions? (C++ language functions can be overridden and overloaded, C doesn't have this capability.) – Thomas Matthews Dec 18 '15 at 20:59
  • 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 Dec 18 '15 at 21:02
  • 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 Dec 18 '15 at 21:21
  • 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 Dec 18 '15 at 21:27
  • C is not C++ is not C! – too honest for this site Dec 18 '15 at 22:55

1 Answers1

2

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.

Community
  • 1
  • 1
Gabriel Ilharco
  • 1,649
  • 1
  • 21
  • 34
  • 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 Dec 18 '15 at 21:21
  • Thanks. It says "Successfully generated all binary outputs" .... But I just have one more question. If I want to call the compiled function ( say it's declaration is Func1(int x, int y) ), how do I call it now in a new C file ? – Ahmad Shah Dec 20 '15 at 13:30