Discussion:
VC++ 2008 express error C2059: syntax error : 'string' extern "C" in C++ files
(too old to reply)
xhm
2010-01-15 17:29:33 UTC
Permalink
Hi

I am trying to build a .dll in VC++ 2008 express (SP1) and then call
it from Matlab. I have 'extern "C"' in my hearder files .h, and have
implementation in my .cpp files, but when build, I got the below
errors:

error C2059: syntax error : 'string'
error C2238: unexpected token(s) preceding ';'

which point to the line with 'extern "C"' in the header file.

I have searched on the Internet and saw that people generally said
that this was because it was not allowed in C source file but should
work in C++. but my source files are already C++ files (.h and .cpp).
So what's the problem?

p.s., if I replaced the 'extern "C"' with 'static', I can build
successfully in VC++ but the .dll produced can't be called from
Matlab, that's why I would like to try 'extern "C"'

Any help on this will be appreciated.

p.s., my header file:

namespace MathFuncs
{
class MyMathFuncs
{
public:
//Returns a + b
// If use 'extern "C" ' to replace 'staic', the DLL can't be built
successfully in Visual C++ 2008 express,
extern "C" __declspec(dllexport) double Add(double a, double b);

//Returns a - b
extern "C" __declspec(dllexport) double Subtract(double a, double
b);

//Returns a * b
extern "C" __declspec(dllexport) double Multiply(double a, double
b);

//Returns a / b
//Throws DivideByZeroException if b is 0
extern "C" __declspec(dllexport) double Divide(double a, double b);
};
}
Jonathan Wilson
2010-01-16 03:42:15 UTC
Permalink
Post by xhm
p.s., if I replaced the 'extern "C"' with 'static', I can build
successfully in VC++ but the .dll produced can't be called from
Matlab, that's why I would like to try 'extern "C"'
You cant use 'extern "C"' on a C++ class or its member functions.
Loading...