Solved

Calling C routine from C++

Posted on 2010-08-31
7
762 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

831 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