Link to home
Start Free TrialLog in
Avatar of jinjool
jinjool

asked on

Building dll in Microsoft C++

HI,

I have been trying to build DLL in MS C++ using extern "C".
Here is my simple code:

#include <stdio.h>^M
#include <math.h>^M
^M
/* extern "C" */^M
extern "C" _declspec(dllexport) void calc1(float,float);^M
^M
void calc1(float x,float y)^M
/* test subroutine for building library */^M
{^M
float z;^M
^M
z= x + y;^M
printf("x=%.2f y=%.2f z=%.2f\n",x,y,z);^M
^M
/* end calc1 */^M
}^M

I just want to compile this in command line mode.
To compile from command line, we used^M
^M
cl -c calc1.c^M
^M
We get the following error message:^M
^M
calc1.c(5) : error C2059: syntax error : 'string'^M

Why is this happening? What am I doing wrong?

Thank you!

JJ


Avatar of peter_vc
peter_vc

JJ,

I thought you only had to use the extern "C" directive in cpp files.  I believe the compiler is smart enough to know that you are compiling a file with a .C extension, and therefore you should not require the "C".

Peter


peter_vc, I think you found the problem, but you expained it backwards.  The problem is that with a ".c" extension it compiles the program as a C program not a C++ program.  But the solution is to change the file to have a ".cpp" extension.  That way it is compiled as a C++ program.  (I assume that jinjool intended for it to be a C++ program, or he wouldn't have C++ syntax.)
Compiler error C2059 is usually a Syntax Error. line 5 is the printf() command. maybe it's not the extern "C" that's the problem...
That line seems fine and is nowhere near 5.
Are you using Microsoft C/C++ 7.0 or Microsoft Visual C++? Which version?

Line 5 is extern "C" _declspec(dllexport) void calc1(float,float);
Avatar of jinjool

ASKER

Thank you all for your comments.
Yes, nietod was right. The problem was-- the person who
did the program was running it without a .cpp extension.
I should have known it but somehow completely overlooked
the error.

Thanks again,
JJ
So, the points should be given to nietod.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial