I am trying to run a simple mex example in matlab. The .c program is
#include <math.h>
#include <matrix.h>
#include <mex.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
printf("Hello World!\n");
}
When I try the command "mex helloWorld.c" I get the following error message:
/home/rory/com/machine: Command not found.
Warning: You are using gcc version "4.7.2-2)". The version
currently supported with MEX is "4.4.x".
For a list of currently supported compilers see:
http://www.mathworks.com/support/compilers/current_release/
I understand the second error message. But I have no idea what the first means. Rory is not my name (I am on a work pc). What specific command is being sought after and where can I find the option to change to a more appropriate path?
Thanks,
Thomas
[update] - I have replaced printf with mexPrintf, as suggested. The error remains, but it seems to compile and run correctly. Running -setup produces the same error, but otherwise informs me of the options file currently in use as it normally does.
At the moment, it looks like I can ignore the error.
Thanks again.
Related
I realize a CMSIS Project solution with VS Code but I've an error on an include file :
#include "stm32f10x.h"
And I've got this error :
In included file: "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"clang(pp_hash_error)
core_cm3.h(90, 6): Error occurred here
The path of this file is here :
C:\Users\"name"\AppData\Local\Arm\Packs\Keil\STM32F1xx_DFP\2.4.0\Device\Include
But I think I forgot something during the configuration.
I justa want to build my C file but VS code doesn't make the link between my .h and my CMSIS project.
You are never supposed to #include "stm32f10x.h"
You only #include "stm32f1xx.h" and it will include the other headers you need.
You must also define a macro on the command line, one of STM32F101x6, STM32F101xB, STM32F101xE or STM32F101xG.
For most compilers you can define this with an argument like -DSTM32F101xB.
After that you will need particular command line arguments that match your chosen processor, such as -mthumb -mcpu=cortex-m3 -mfpu=none.
Maybe your error was specifying an incorrect -mfpu=.
I am trying to access C++ code from Matlab R2018a but getting error
Invalid MEX-file 'C:\\C++ForMatlab\test.mexw64': Gateway function is
missing.
I did the similar thing on Windows 7 with VS2013 and Matlab 2016 and it all worked fine.
The function compiles successfully and dll is created as expected but when I try to run it I got error message.
OS --> Windows 10
Matlab --> Matlab R2018a
C++ -> VS 2017
Sample code written in VS2017
FileName --> test.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello World!\n");
}
In Matlab
I type following command
mex test.cpp
And Output I get is
Building with 'Microsoft Visual C++ 2017'.
MEX completed successfully.
But After that when I try to run by typing
Trial>> test
I get following error
Invalid MEX-file 'C:\C++ForMatlab\test.mexw64': Gateway function is
missing
.
Generate a file "ThisModule.def" and add following lines.(swap templateSFunc with your own file name)
LIBRARY "templateSFunc.mexw64"
EXPORTS
mexFunction
Then go to project properties in VS and add ThisModule.def to the linker/Input/Module Definition File option.
First I wanted to compile MatConvNet library for using in windows form this tutorial
(Compiling MatConvNet on Windows)
but I couldn't. Then I think it is better to compile a very simple file and after that going to compile the library.
I have Matlab R2013a 64 bit and Visual Studio 2010 64 bit.
my program Test.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
printf("Hello! :)\n");
}
I can compile Test.cpp in matlab with mex Test.cpp
And when I type test output is Hello! :)
I also can set the correct configuration according to tutorials below and compile it without errors.
1) http://coachk.cs.ucf.edu/GPGPU/Compiling_a_MEX_file_with_Visual_Studio2.htm
2) http://www.orangeowlsolutions.com/archives/490
But when I run it in Matlab, nothing happen. There is no output and Matlab doesn't give me any error.
What is the problem?
Notice that:
in (1) second step is adding "mexversion.rc" from "matlab\extern\include" to the project But this file dose not exist in my computer so I couldn't do it.
In Visual Studio I needed to add tow headers in below for compiling the program.
include "stdafx.h"
include "maxrix.h"
so the Test.cpp in Visual Studio is:
#include "mex.h"
#include "stdafx.h"
#include "matrix.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
printf("Hello! :)\n");
}
Pre-compiled header shenanigans
A problem with the Visual Studio version of the code is the pre-compiled header file stdafx.h causing the compiler to ignore any code above it (the mex.h include):
#include "mex.h"
#include "stdafx.h" // ANYTHING above here is IGNORED!
#include "matrix.h"
Move the stdafx.h include to the top or turn off PCH in projects settings and remove the include.
printf vs. mexPrintf
Before getting into MEX project setup, note that printf points to mexPrintf courtesy of mex.h:
#define printf mexPrintf
So, using printf is not an issue, but probably not good practice. The problem comes if you redefine printf after including mex.h or fail to get this define on account of the PCH header.
Regarding MEX in Visual Studio
I posted a more formal guide to setting up a Visual Studio projects for building MEX files as an answer to the more commonly-reference question on this topic, and I would also suggest here to use Visual Studio property sheets to get your project set up to build a MEX file. The details are in the referenced post, but you just need to:
Set the MATLAB_ROOT environment variable.
Create a new DLL project.
Under Property Manager (from View menu), right click on each project's build configuration and "Add Existing Property Sheet...", choosing the MATLABx64.props file from this GitHub repo.
printf only works in native C. You need to use mexPrintf. Therefore, your code should be this:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello! :)\n");
}
In general, printing to standard output in a MEX script doesn't appear in the MATLAB command prompt. If you want to display messages in MATLAB, you need to use mexPrintf instead of printf.
To be explicit, if you consult the mexPrintf documentation, a caveat can be seen towards the end:
In a C MEX-file, you must call mexPrintf instead of printf to display a string.
BTW, I recommend this awesome MEX tutorial here: http://classes.soe.ucsc.edu/ee264/Fall11/cmex.pdf. This is the tutorial that I used to get started with programming MEX wrappers in MATLAB. You'll also see that the first example is of the same "Hello World" caliber that you are trying to get running :)
Good luck!
How To Compile a C/CPP File and Create mex File for Using in Matlab
I found the solution by #rayryeng help and (How to build mex file directly in Visual Studio?) #jorre's post.
This is tested with Matlab R2013a 64 bit and Visual Studio 2010 64 bit.
Test.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello World123! :)\n");
}
1.Create a new project in VS -> Visual C++ -> General -> Empty Project.
The name of your Project will be the name of mex file.
You can change it later.
2.Change the Debug to Release.
Right click on the project -> Properties -> Configuration properties -> General
Set Target Extension to .mexw64
Set Configuration Type to Dynamic Library (.dll)
Configureation poperties -> VC++ Directories:
5.Add $(MATLAB_ROOT)\extern\include; to Include Directories
Configuration properties -> Linker -> General:
Add $(MATLAB_ROOT)\extern\lib\win64\microsoft; to Additional Library Directories
Configuration properties -> Linker -> Input:
Add these things to Additional Dependencies
libmx.lib
libmex.lib
libmat.lib
Configuration properties -> Linker -> Command Line:
Add /export:mexFunction to Additional Options
Now you must set you platform to x64 otherwise you'll get error like"Error 1 error LNK2001: unresolved external symbol _mexPrintf".
9.Configuration Properties -> Configuration Manager -> Active Solution Platform -> New -> x64 -> Copy Settings From Win32
Now you can compile your file and get mex file.
If you see these tutorials there are other things thar are not needed and may cause problems.
(http://coachk.cs.ucf.edu/GPGPU/Compiling_a_MEX_file_with_Visual_Studio2.htm)
Create an empty Project, not a MFC Dll Project.
There is no need to *.def file.
There is no need to add "mexversion.rc" into your project.
There is no need to add "MATLAB_MEX_FILE" as a preprocessor definition.
I have a question related to debugging .mex32/.mex64 files. Suppose now I have a file named test.cpp:
#include "mex.h"
#include <iostream>
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello Matlab, and this is a test program\n");
}
I can then compile and build test.mex64 with Visual Studio 2010.Then in matlab, I can write the following script to test the function:
clc;
test;
Now suppose I want to debug the test.mex64 function, what should I do? The have adopted the following procedure, but failed:
Toggle break point at the begging of the line mexPrintfwith VS2010.
With VS2010 from Debug->Attach to Process... select MATLAB.exe.
Run MATLAB script clc; test;
The error message I have obtained is as follows:
The breakpoint will not currently be hit. No symbols have been loaded for this document.
Did you build your mex file with debug option "-g"?
I have found the solution: when I created the .mexw64 function (test.mexw64 in our case), I copied it to the MATLAB work directory. In order to debug this function, it is important to copy test.pdb file to the MATLAB work directory as well. After doing that, I can debug.
This might be a very dumb question...
I'm trying to use Boost.Python in an Xcode project (boost 1.50, xcode 4.4, OS X 10.8). I installed both boost and python through macports. I dragged the macports Python framework (/opt/local/Library/Frameworks/Python.framework) into the project and tried the most minimal program possible:
#include <boost/python.hpp>
int main(int argc, const char * argv[]) {
Py_Initialize();
Py_Finalize();
return 0;
}
and get:
/opt/local/include/boost/python/detail/wrap_python.hpp:50:11: fatal error: 'pyconfig.h' file not found
Do I need to explicitly add the framework header folder to the project's header search paths? That seems irregular... so hopefully I'm just misunderstanding something?