Solved

Build DLL with C code in Visual C++

Posted on 1998-10-23
15
369 Views
Last Modified: 2010-04-02
i want to make a DLL out of some existing C code.
i am having problems because it really is C code, not C++

for example, it has functions specified in this manner:

foo (x, y)
char *x, **y
{
... code ...
}

when i try to create the DLL in Visual C++,
it can't compile this old C style stuff.

what can i do?
john
0
Comment
Question by:mickeydog
  • 9
  • 6
15 Comments
 

Author Comment

by:mickeydog
ID: 1175865
i should add that the compile errors i get:

error C2065: 'x' : undeclared identifier
error C2065: 'y' : undeclared identifier
error C2448: '<Unknown>' : function-style initializer appears to be a function definition
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1175866
Standard C code is compilable as standard C++ code with no changes.  Unfortuantely, that code is so old that it is not standard C code.

The problem is that in the old days if you did not specify a type, the compiler assumed type int.  This was a very bad idea.  

Just declare x and y as int.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1175867
Opps, actually there types are declated, but not in the function's parenthesis.  Again this is an old way of doing this and it is not legal even in C!

Just move the "char *x, **y" into the parentheseis to get

foo (char *x,char ** y)
{
  ... code ...
}
0
 

Author Comment

by:mickeydog
ID: 1175868
hmmm...i was told, when given the existing C files, that they compiled.
so i am a little nonplussesd about that

and yes you are correct that they compile when changed as you say, but
i should have said that i didn't want to change them because having 2 copies of the same code increases the maintenance.

i guess i don't have a choice.


0
 

Author Comment

by:mickeydog
ID: 1175869
hmmm...i was told, when given the existing C files, that they compiled.
so i am a little nonplussesd about that

and yes you are correct that they compile when changed as you say, but
i should have said that i didn't want to change them because having 2 copies of the same code increases the maintenance.

i guess i don't have a choice.


0
 
LVL 22

Expert Comment

by:nietod
ID: 1175870
The change I proposed will work under both C and C++.  If that code compiled under C, it is because the compiler is very old and has not been updated, or because it has a feature that allows it to compile old (non-standard) code.  It should be able to compile the code I suggested unless it is a very old compiler.
0
 

Author Comment

by:mickeydog
ID: 1175871
also, when i have created Dlls in VC++ before (using C++ i created, i had to export the functions using the statement:  
__declspec( dllexport ) void _stdcall

is using this statement how to do it in this case?

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:mickeydog
ID: 1175872
i'll answer that last question myself
yes

thanks for your help. its nice to talk to someone with the depth of experience you obviously have.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1175873
>>its nice to talk to someone with the depth of experience you obviously

Want to buy a bridge?

.I've been programming in C++ for about 2 years now and never in C.

I should mention that when you export from C++ using __declspec(dllexport), you are pretty much requiring that the exported procedure be used only from a program (or DLL) that is also written in C++.  You will not (realistically) be able to call the exported procedure from other languages (even C).  In fact it may not be callable from C++ EXEs or DLLs that are written using different C++ compilers.  Ussually this is not a problem.  But I wanted to warn you.
0
 

Author Comment

by:mickeydog
ID: 1175874
>>I've been programming in C++ for about 2 years now and never in C.
just a genius, then, i suppose  ;)

i don't think i need to, but just out of curiousity, is there an alternative to  
__declspec(dllexport) that would allow exporting to C?

0
 
LVL 22

Expert Comment

by:nietod
ID: 1175875
Along with __declspec(dllexort) you must use the "extern "C" " declaration.  In C++ the name you give the procedure is not the actual procedure name used by the compiler and linker.  C++ allows procedures to be overloaded.  That is, in C++ two procedures can have the same name if they have different parameters.

 Well, in practice, that is not reallly possible.  So C++ lets you give the procedures the same name in the source code, but it gives them "decorated" names that are unique.  The "decorated" ussualy start with the name you gave and are then followed by a bunch (often 100's) of characters that record the parameters to the function.  Thus the decorated names are guaranted to be unique.  This is invisible to you in C++ because you don't care what name the compiler actually uses, but if you export the function, then you care.  if a another program tries to find exported function using the non-decorated name, it won't find it.  It could fidn it using the decorated name, but that is really ugly.  

So instead, you can use extern "C to tell C++ to dissable decoration on a specific function.  In that case the function will be exported with the name you gave it.  That looks like

extern "C" _declspec(dllexport) int somefunction(int i)
{
  return i + 1;
};
0
 

Author Comment

by:mickeydog
ID: 1175876
ah, it is all so clear now...thanks.

btw, the old/non-standard compiler issue is that the old code is written to the Kernigan-Ritchie standard.  they have a switch on the compiler to turn K-R on/off.
i don't suppose Visual C++ has something like that, it is ANSI-C, correct?

0
 

Author Comment

by:mickeydog
ID: 1175877
ah, it is all so clear now...thanks.

btw, the old/non-standard compiler issue is that the old code is written to the Kernigan-Ritchie standard.  they have a switch on the compiler to turn K-R on/off.
i don't suppose Visual C++ has something like that, it is ANSI-C, correct?

0
 
LVL 22

Expert Comment

by:nietod
ID: 1175878
VC is ANSI standard only.  (Well, close to it anyways).  But it handles C and C++.   If a file has a ".c" extension it compiles it as a standard C program (no C++ extensions).  If a file has a ".cpp" extension, it compiles it as standard C++ program.  
0
 

Author Comment

by:mickeydog
ID: 1175879
i have posted another question related to what we've been talking about.
i would just assume to ask you, but i wanted to post it so you can get more points.

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now