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...
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixing C++ & C# in Vis Studio 2013 7 177
How to convert MFC::CString to UTF8 wchar_t* 10 211
What is sub-make ? 2 61
How to Correctly derive class from CWinThread in MFC 4 41
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

809 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