Building dll in Microsoft C++

Posted on 1999-01-28
Last Modified: 2010-04-16

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!


Question by:jinjool

Expert Comment

ID: 1185448

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".


LVL 22

Expert Comment

ID: 1185449
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.)

Expert Comment

ID: 1185450
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...
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

LVL 22

Expert Comment

ID: 1185451
That line seems fine and is nowhere near 5.
LVL 23

Expert Comment

ID: 1185452
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);

Author Comment

ID: 1185453
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,
LVL 23

Expert Comment

ID: 1185454
So, the points should be given to nietod.
LVL 22

Accepted Solution

nietod earned 50 total points
ID: 1185455
Answering as suggested by Chensu (Thanks).  (Sorry, for the delay, I was out of town.)

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 117
basic hardware to learn oop advanced design patterns 3 101
Exception thrown at 0x00007FFD5BC81F28 7 49
VS2015 Redefinition errors 4 70
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question