I am trying to use the MinGW-w64 Compiler in Matlab. I have it installed and Matlab recognizes the compiler. My question is 2-fold:
1) I cannot get it to compile all the .c code into a header. I typed
mex -output HTKToolsFolder *.c
Building with 'MinGW64 Compiler (C)'.
And got
Error using mex
C:\Users\username\Documents\MATLAB\MatlabHTK\htk\HTKTools\HBuild.c:39:46: fatal error: HShell.h: No such
file or directory
#include "HShell.h" /* HMM ToolKit Modules */
^
compilation terminated.
I'm not sure what the .h extension is. That file is located in another folder but if I tell Matlab to compile .h files it doesn't recognize the file extension
mex -output hfilescompiled *.h
Error using mex
Unknown file extension '.h'.
If it helps, HShell.h is called by some of the .c files
The second question is, once question #1 is answered, can I apply this to a directory with many folders of C++ code? Or do I have to use the mex command for every single folder?
I've watched the Matlab tutorial: https://www.mathworks.com/solutions/matlab-and-c.html and I've also read the Matlab help files for mex and loadlibrary.
Any advice is greatly appreciated as this is the first time I've mixed C++ and Matlab. Thanks in advance!
You want to do this:
mex HBuild.c -IC:\Path\To\H\Files
where C:\Path\To\H\Files is whatever the directory is that contains HShell.h.
This will create a MEX-file called HBuild (assuming that HBuild.c contains a mexFunction function, and no other dependencies exist).
If you do mex *.c, then all C source files in that directory will be combined into a single MEX-file. I doubt that this is what you want. You need to call mex for each of the MEX-files you want to build. Each MEX-file will become a function available within MATLAB.
Related
I have a simulink model with a c++ s-function.
This s-function needs access to a lot of (>50) classes. Each class is consists of a header (.h) and a source (.cpp) file.
I have also divided my code into multiple directories:
root
-sfun.cpp
-folder1
--file1.h
--file1.cpp
--file2.h
--file2.cpp
-folder2
--file3.h
--file3.cpp
...
For the compilation of the s-function I am using the mex-function:
mex -Ifolder1 -Ifolder2 -outdir bin sfun.cpp folder1/file1.cpp folder1/file2.cpp folder1/file3.cpp
(http://de.mathworks.com/help/matlab/ref/mex.html)
But this gets very long and ugly with more and more files because I need to specify each header folder and earch source file separately.
Is there a better way to create a mex file that needs access to lots of source files?
I have the following ideas, but I am not sure what could be the correct and easiest way:
Add all header and source files (fileX.h/ fileX.cpp) to an visual studio project and compile them to a *.lib file. Then compile only the sfun.cpp with the mex tool and provide access to the *.lib file
Move all header and source files into one directory. This would shorten the command line as follows:
mex -outdir bin sfun.cpp file1.cpp file2.cpp file3.cpp
Making everything inline so that there is no need for a source file. (very ugly solution)
Is there some kind of a makefile for the mex compiler?
Include not only the header files but also the source files via a #include directive.
At the moment I am not convinced of any of these ideas and I would appreciate any help.
Thanks
Edit1:
One annotation: This project should be ported to a dspace pc in a later stage. Do I need to consider something special in this case?
I have a source code containing .c files which is built using ndk build in eclipse Ide. I want to add a .cpp file , in which i need to import my .c files. I get compile time issues when I do the same. Most of the issues are due to type casting to user defined data types. The compilation runs fine, and ndk -build is successful with only .c files
jni/folder1\folder2\folder3\folder4\src\abc.c:963:29: error: invalid conversion from 'int' to 'MY_STATUS_CODE' [-fpermissive]
How can I solve this?!
You can't include C code in a C++ source file unless the C code is valid C++. The compilation error shows that your C code is not valid C++.
You could try to work around the problem by changing your C code to be also valid C++ but that's a waste of time in my opinion. The proper way to solve the error is to not include the C source file but compile it separately. Including a source file in another is bad design anyway so you'll get rid of that smell for free.
I want to use the file exchange about kd-tree in matlab and search in mathwork site and saw the below m-files but I cant understand how can I mex files.
in comments "Kuan-Ting Yu" say: 1. use mex -setup to find your compiler. E.g. VS 2010
2. in "kdtree_common.h", replace #include "c:/.../mex.h" with "mex.h"
3. dir to ./src and mex all .cc file
what is his mean?
haw can I mex all .cc file?
http://www.mathworks.com/matlabcentral/fileexchange/4586-k-d-tree
To mex all source files, you have to call the mex function.
To call mex for all cc-files in a directory, use
for file = dir('*.cc'); mex(file.name) ; end
I am trying to use the dynamic bitset provided by boost libraries. Downloaded the file boost_1_55_0.tar.bz2 and extracted it into a folder named boost. In this folder I have put my source code file with the #include directive #include <boost/dynamic_bitset.hpp>, when I compile the source code, the compiler returns "No such file or directory". Where to place the source code?
I managed to compile the program by putting the source code file in the the same directory that boost is in, and compiling using the command formula:
g++ -I /your/source/root /your/source/root/A/code.cpp
As mentioned in the How to make g++ search for header files in a specific directory?
I need generate Dll file from matlab cods and use this in C++. I can not couple dll file in c++. Please help me.
This looks promising: How do I create a C - shared library with MATLAB Compiler 3.0 which can be used in other projects?
In short:
Compile your MATLAB files into a DLL (on Windows): mcc -t -L C -W lib:mylib -T link:lib -h <MATLAB files> libmmfile.mlib
Add mylib.lib to your MSVC (or your own IDE) project
Make sure to call the initialization and termination routines from your
code before calling any of the MATLAB files that are compiled. You need to call: mylibInitialize();
Afterwards, you should call the termination routine: mylibTerminate();
All of the symbols in mylib.dll will also appear in mylib.h.
You can call the functions compiled from the MATLAB code by invoking mlfFoo(...), from your C code.