Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Calling C routine from C++

Posted on 2010-08-31
7
Medium Priority
?
779 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

927 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