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

x
?
Solved

Calling C routine from C++

Posted on 2010-08-31
7
Medium Priority
?
772 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
[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
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
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.

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