Solved

Calling C routine from C++

Posted on 2010-08-31
7
759 Views
Last Modified: 2013-11-27
I am trying to integrate the RSA MD5 hash code, written in C, into my C++ project (Visual C++ 2008 Express).  The project has a separate file with the MD5 code, and compiles fine.  However I get a link error when I try to call it from my C++ code.  The link error is:

error LNK2019: unresolved external symbol "void __cdecl MD5Init(struct MD5Context *)" (?MD5Init@@YAXPAUMD5Context@@@Z) referenced in function _com_GetHash

(actually there are 3 routines that fail to link, this is just one)

I assume the problem is related to how I have the routines defined, but nothing I try seems to fix it.  The routine referenced above is defined in the header file (included in both the C and C++ modules) as:

extern void MD5Init(struct MD5Context *);

Any ideas?


0
Comment
Question by:jmckay321
  • 3
  • 3
7 Comments
 
LVL 8

Accepted Solution

by:
Anthony2000 earned 500 total points
ID: 33572964
When you want to link in c routines defined in a ".c" file into a c++ program you need to do the following in your header file that you include in your c++ source file. This is because of name mangling. The c++ compiler alters the names of functions (under the covers) and the linker will know how to link these mangled names. So, if you include a header file and don't tell the c++ compiler to use "c" names, then it will default to the name mangling. You can google c++ name mangling and you can find a detailed explanation of what it is.

I copied this from an old Visual C++ program. I think this will work with Visual Studio Express.

#ifdef __cplusplus
extern "C" {
#endif

extern void MyCFunction(void);

#ifdef __cplusplus
}
#endif


0
 
LVL 32

Expert Comment

by:phoffric
ID: 33572967
Try this to let the C++ compiler know that the C linkage is required:
    extern "C" void MD5Init(struct MD5Context *);

You can also try to make the C routine a C++ routine by changing the file extension from .c to .cpp. But then there may be compiler errors/warnings that you may have to deal with. If not too many, that is a good way to go as well (but now you have to thoroughly retest the new .cpp module).
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 33572970
I check the Microsoft docs for predefined macros and __cplusplus is correct.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:Anthony2000
ID: 33572975
Here is a good explanation of name mangling in c++:

http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_C.2B.2B
0
 
LVL 32

Expert Comment

by:phoffric
ID: 33572981
>> check the Microsoft docs for predefined macros and __cplusplus is correct
Actually, no need to check, since it is defined in the C++ standard:

16.8 Predefined macro names
_ _cplusplus The name _ _cplusplus is defined to the value 199711L when compiling a C + + translation unit
0
 
LVL 32

Expert Comment

by:phoffric
ID: 33572987
So, using __cplusplus is good for any compiler/liner that is compliant to the ISO/IEC 14882 C++ standard.
0
 

Author Closing Comment

by:jmckay321
ID: 33573429
Thank you all for your comments - I do recall the issue now, needed a refresher course...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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 …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

920 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

16 Experts available now in Live!

Get 1:1 Help Now