Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Build DLL with C code in Visual C++

Posted on 1998-10-23
15
Medium Priority
?
412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

660 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