Building dll in Microsoft C++


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
/* extern "C" */^M
extern "C" _declspec(dllexport) void calc1(float,float);^M
void calc1(float x,float y)^M
/* test subroutine for building library */^M
float z;^M
z= x + y;^M
printf("x=%.2f y=%.2f z=%.2f\n",x,y,z);^M
/* end calc1 */^M

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

Why is this happening? What am I doing wrong?

Thank you!


Who is Participating?
nietodConnect With a Mentor Commented:
Answering as suggested by Chensu (Thanks).  (Sorry, for the delay, I was out of town.)

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_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.)
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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);
jinjoolAuthor Commented:
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,
So, the points should be given to nietod.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.