Solved

Calling C routine from C++

Posted on 2010-08-31
7
756 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
Comment Utility
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
Comment Utility
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
Comment Utility
I check the Microsoft docs for predefined macros and __cplusplus is correct.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 8

Expert Comment

by:Anthony2000
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
Thank you all for your comments - I do recall the issue now, needed a refresher course...
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

744 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

13 Experts available now in Live!

Get 1:1 Help Now