Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Compiling C functions with MEX

Subject: Compiling C functions with MEX

From: DC

Date: 1 Feb, 2011 19:33:03

Message: 1 of 5

Hello,
I have some problems trying to compile a program with MEX.
I have a main function in one file that calls other functions, in other files.
I tried to compile only the main function but its giving some link error, and I tried to compile also the main function together with the other function plus the header files, still not working.
So my question is: when i compile the main function, I compile it together with the other functions and with/without the headers?
Can you guide me a little through the process please.
Thank you

Subject: Compiling C functions with MEX

From: James Tursa

Date: 1 Feb, 2011 23:03:03

Message: 2 of 5

"DC" wrote in message <ii9n5f$rj7$1@fred.mathworks.com>...
> Hello,
> I have some problems trying to compile a program with MEX.
> I have a main function in one file that calls other functions, in other files.
> I tried to compile only the main function but its giving some link error, and I tried to compile also the main function together with the other function plus the header files, still not working.
> So my question is: when i compile the main function, I compile it together with the other functions and with/without the headers?
> Can you guide me a little through the process please.
> Thank you

Put all the source files into one directory. Make that your current directory. Then mex all the source files (but not anything that is #included in your source files, like header files) on the mex command line. e.g.,

mex main.c other1.c other2.c other3.c

Your MATLAB function would then be called "main" since it is the first file in the above list.

James Tursa

Subject: Compiling C functions with MEX

From: DC

Date: 2 Feb, 2011 09:26:02

Message: 3 of 5

Thank you very much for your reply. I have tried to compile it this way and I get the following errors:
signalProcessing.obj : error LNK2019: unresolved external symbol _hDevice referenced in function _WriteDone@12
signalProcessing.obj : error LNK2019: unresolved external symbol _hFile referenced in function _ReadDone@12
main.mexw32 : fatal error LNK1120: 2 unresolved externals
 
  C:\PROGRA~1\MATLAB\R2010A\BIN\MEX.PL: Error: Link of 'main.mexw32' failed.

I f i have a resource file(.rc) should I include that one too in the compilation?
Do you have any idea what is causing the errors?
Thank you.

Subject: Compiling C functions with MEX

From: DC

Date: 2 Feb, 2011 09:44:03

Message: 4 of 5

I managed to solve it. thank you.
I had a global variable defined inside the mexFunction. Once I out it outside things started working.
Thank you for your help.

Subject: Compiling C functions with MEX

From: Rune Allnor

Date: 2 Feb, 2011 11:16:40

Message: 5 of 5

On Feb 1, 8:33 pm, "DC " <dcata...@es.aau.dk> wrote:
> Hello,
> I have some problems trying to compile a program with MEX.
> I have a main function in one file that calls other functions, in other files.
> I tried to compile only the main function but its giving some link error, and I tried to compile also the main function together with the other function plus the header files, still not working.
> So my question is: when i compile the main function, I compile it together with the other functions and with/without the headers?
> Can you guide me a little through the process please.
> Thank you

In MEX you should *not* have a main() function defined
by you. That's already taken care of in the main matlab
executable.

Replace main() by a properly formatted mexFunction(),
as specified in the Matlab API/External Interfaces Guide.

Rune

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us