Mex compilation with header files
이전 댓글 표시
Hi,
I'm running Matlab on Ubuntu 12.04. I have written a C++ function simulation.cpp using the mex format. I've defined some of my functions in a separate header file that is located in the same directory as simulation.cpp. I read on the web that to compile a .cpp file with header files, I need to use -l and provide the path of the header files. When I run the following:
mex -l"/media/.../folder_containing_header_files" simulation.cpp
I get the following error
/usr/bin/ld: cannot find -l/media/.../folder_containing_header_files/
collect2: ld returned 1 exit status
mex: link of ' "simulation.mexglx"' failed.
Incidentally, all my header files are located in the same directory as simulation.cpp, which is also my working directory in matlab.
Does anybody know what the problem is?
Thanks!
답변 (5개)
Sumeet
2013년 1월 17일
Thank You all for your help.
James: I do have all my header files in one folder. When I tried without the -I as you suggested, I was getting an undefined reference error for all functions declared in my header file and defined in a separate cpp file (with the same name as the header file). Some web-searching revealed that I shouldn't be doing just
mex simulation.cpp
but instead
mex simulation.cpp header.cpp
This worked. Sorry if this is well-known, I wasn't aware of this - I don't have a CS or compilers background.
Thanks again James, Walter and Ryan!
댓글 수: 1
Letian Wang
2015년 1월 29일
Hi, I am also having this problem. But I did have a header.hpp file, and it could not be recognized by matlab.
So how can you make it? turn it into cpp file?
Thank you for sharing!
Walter Roberson
2013년 1월 15일
1 개 추천
You used the "-l" (lower-case L) option; you need to use "-I" (upper-case I). "I" for "include".
Also you might need to use a space between the option and its value.
댓글 수: 1
Thanks again Walter. Yes, -I (I for include) was the problem. However, now it cannot find mex.h. Where is it located and how do I specify its location to matlab?
>> mex -I "./" simulation.cpp
Warning: You are using gcc version "4.6.3-1ubuntu5)". The version
currently supported with MEX is "4.4.6".
For a list of currently supported compilers see:
http://www.mathworks.com/support/compilers/current_release/
simulation.cpp:2:17: fatal error: mex.h: No such file or directory
compilation terminated.
mex: compile of ' "simulation.cpp"' failed.
Error using mex (line 206)
Unable to complete successfully.
James Tursa
2013년 1월 15일
0 개 추천
If your current directly is the directory that contains simulation.cpp and its associated header files, doesn't simply "mex simulation.cpp" work at the command line?
댓글 수: 3
Walter Roberson
2013년 1월 15일
Possibly yes.
#include "HeaderFile.hpp"
would look in the current directory, but
#include <HeaderFile.hpp>
would not look in the current directory, in which case the -I option would be needed.
Sumeet
2013년 1월 15일
Thanks James and Walter. I tried both `<HeaderFile.hpp> and "HeaderFile.hpp". It didn't help.
Walter Roberson
2013년 1월 15일
Are you still using -l (lower-case L)? I'm sure it should be -I (upper-case I)
James Tursa
2013년 1월 15일
편집: James Tursa
2013년 1월 15일
Have you tried the function form of mex yet? I.e., something like
mex('-I','...whatever...','simulation.cpp')
Also, what do your include lines in the actual source code look like?
댓글 수: 7
Sumeet
2013년 1월 15일
Same error.
>> mex('-I','./','simulation.cpp')
Warning: You are using gcc version "4.6.3-1ubuntu5)". The version
currently supported with MEX is "4.4.6".
For a list of currently supported compilers see:
http://www.mathworks.com/support/compilers/current_release/
simulation.cpp:2:17: fatal error: mex.h: No such file or directory
compilation terminated.
mex: compile of ' "simulation.cpp"' failed.
Error using mex (line 206)
Unable to complete successfully.
Here are my actual include lines:
#include <math.h>
#include "mex.h"
#include <iostream>
#include <iomanip> //for setw
#include <map>
#include <string>
#include <sstream> //for string stream
#include "example4channel.h" //my header file
I tried `<mex.h> (instead of "mex.h") as well, didn't help.
James Tursa
2013년 1월 15일
What happens if you try to compile this file:
// gateway.c
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
}
with just this command:
mex gateway.c
Sumeet
2013년 1월 15일
Surprising. It works.
>> mex gateway.c
Warning: You are using gcc version "4.6.3-1ubuntu5)". The version
currently supported with MEX is "4.4.6".
For a list of currently supported compilers see:
http://www.mathworks.com/support/compilers/current_release/
I also tried a sample cpp file using mex code that I found online, and it worked. The only difference being, the user hadn't created his own header files. So the problem occurs because I have created my own header files. I haven't found an example containing header files so far - that would help me figure out what is wrong with my code.
James Tursa
2013년 1월 15일
I would suggest adding a header file (e.g., an empty one) to the gateway.c example above and try that. Then successively build things up until you discover the problem.
Sumeet
2013년 1월 16일
The problem occurs even before I add the header file. When I do mex gateway.c, it compiles without error, but when I do mex -I "./" gateway.c, it complains of a missing mex.h file. So one guess is that it is trying to search for mex.h within my current directory. How do I tell matlab to not restrict its search to my directory, but add my directory to the list it already searches?
James Tursa
2013년 1월 17일
편집: James Tursa
2013년 1월 17일
Frankly, I don't know for sure since I don't personally use any of these library/include/directory options myself. Things like having spaces in directory names etc tend to complicate things, so I avoid these options entirely. But I have never had a problem with the following approach:
- Have all the source and header files in one directory
- Make that directory the current directory
- Use simple #include "filename.h" syntax in source files
- Use filenames without directory info on the mex command line
Does this approach work for you?
Walter Roberson
2013년 1월 17일
If you were to try
mex -v gateway.c
then along the way it would probably indicate which directories it is -I'ing; you could then -I those directories for your actual code.
Ryan Livingston
2013년 1월 17일
There should be no space between the switch and the directory. So can you try:
mex -I./ gateway.c
rather than
mex -I "./" gateway.c
카테고리
도움말 센터 및 File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!