Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

Build DLL with C code in Visual C++

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
mickeydog
Asked:
mickeydog
  • 9
  • 6
1 Solution
 
mickeydogAuthor Commented:
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
 
nietodCommented:
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
 
nietodCommented:
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
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.

 
mickeydogAuthor Commented:
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
 
mickeydogAuthor Commented:
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
 
nietodCommented:
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
 
mickeydogAuthor Commented:
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
 
mickeydogAuthor Commented:
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
 
nietodCommented:
>>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
 
mickeydogAuthor Commented:
>>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
 
nietodCommented:
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
 
mickeydogAuthor Commented:
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
 
mickeydogAuthor Commented:
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
 
nietodCommented:
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
 
mickeydogAuthor Commented:
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
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.

Join & Write a Comment

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.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now