Write C DLL for MATLAB

2019-08-30 16:49发布

问题:

I'm trying to write and compile some C code which I would use frm MATLAB with VS 2012

Here is my header file:

#ifndef _DLLTEST_H_
#define _DLLTEST_H_

#include <iostream>
#include <stdio.h>
#include <windows.h>

extern "C" __declspec(dllexport) int Add(int a, int b);


#endif

And here is implementation:

#include "stdafx.h"
#include "nureader.h"

extern "C" __declspec(dllexport) int Add(int a, int b)
{
  return (a + b);
}

Compilation goes fine, but when I try to load DLL to MATLAB, I getting a strange error:

>> [a,b] = loadlibrary('nureader.dll', 'nureader.h')
Error using loadlibrary (line 419)
Failed to preprocess the input file.
Output from preprocessor is:nureader.h
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\eh.h(27) : fatal error C1189: #error :  "eh.h
is only for C++!"

回答1:

Take a look at VS output

fatal error C1189: #error : "eh.h is only for C++!" 

You want to write a C library, right? so don't include C++ in it. or compile with G++ but since you're using windows I don't think you have that option...

In any case, track down what includes "eh.h" and try without it. If it builds without it - great, if not - you will need to only isolate the C portion of your program. By looking at the code, you don't seem to need anything more than

#include <stdio.h>
#include <windows.h>

So try that.