Solution to call C function module in "VS Development" MFC

Source: Internet
Author: User

Solution to call C function module in "VS Development" MFC

tags (space delimited): "VS Development"

Disclaimer: Reference Please specify source http://blog.csdn.net/lg1259156776/

Description: Recently debugging MFC-based programs, when the introduction of a function through an external C file, once again forget the C file and C + + file differences, directly in the general way, the function declaration into the header file, the function definition into the C file, Then in MFC to refer to the corresponding function by including the header file, but the link can not locate the error, this article is to comb the C + + Call the function function of the method.

First in the C file,. C does not allow any form of C + + calls, so do not expect C to call the function functions defined in the C + + file (unless you are using extern "C" to tell the compiler to compile the encapsulated interface in accordance with C, because in the compiled generated target file, Take the same function name as an example: int foo (float x), the function export symbol for the C interface is different from the function export symbol _foo_float for the C + + interface , because the C + + compiler, in order to implement the function overload, takes the parameter information of the function at compile time. So do not declare in advance can not be called, but also remember the previous time read the programmer's self-accomplishment summary of the example, I just a little forgotten. See the next article for a function that calls C + + in C. Then in turn, because C + + is successor, can contain C, so in C + + syntax There is a declaration function as C code snippet, here are a few ways to explain:

1. Link Error

Test.obj:error LNK2019: unresolved external symbol "void __cdecl deletestack (struct _node *)" (? [Email Protected]@[email protected]@@z], the symbol is referenced in the function _main.

2. Method 1
#ifdef __cplusplus
extern "C"
#endif
void Deletestack (stack stack);

If it is under the C + + compiler, a _cplusplus macro is automatically defined, decorated with extern "C", which represents the specification of the external interface function as C. Of course, you can also use curly braces to indicate collective grooming:

#ifdef __cplusplus  extern "C" {    #endif  void DeleteStack(Stack stack);  void PrintStack(Stack stack);  void Pop(Stack stack);  #ifdef __cplusplus  }  #endif  

3. Method 2

Directly according to the header file, the format of the source file, directly declare the entire header file as C specification:

// CStack.h  extern "C" {  #include "Stack.h";  

The code refers to C + + Call function

Rules for exporting symbols in the target file generated by the source file under the compiler

In addition, specific about the source files in the compiler generated in the target file to export the rules of the symbol, you can see my other post "reading notes" Programmer's Self-accomplishment summary (iii), the specific content I paste as follows:

Because global symbols are globally visible in the linking process, conflicts occur if you write a library file that has the same symbol name as the current target file. To prevent similar symbolic name collisions, the C language under UNIX stipulates that all global variables and functions in the C language source code file are compiled and the corresponding symbol names are preceded by an underscore, "_". But if it is a large software, developed by different departments, if the naming conventions between them are not strict, it can lead to conflict, so the language of C + + uses namespace to solve the symbolic conflicts under different modules.

The symbolic decoration mechanism for C + + refers to functions with the same name as the C + + language that supports different parameter types, that is, function overloading. In fact, they perform a function signature at compile time, containing information such as function name, parameter type, class, and namespace.

The name decoration mechanism, which is also used to prevent static variables, different compiler may name decoration methods, the function signature may correspond to different decorated names, because different compilers adopt different name modification methods, it is inevitable that the target files generated by different compilers will not be linked to each other properly.

"extern C" usage

In C + + in order to be compatible with C, there is a "extern C" keyword usage for symbol management that declares or defines a C symbol:

extern “C”{    int func(int);    int var;}

The C + + compiler treats the code inside the curly braces of the extern "C" as the C language. VC + + Platform will modify the C language symbol, that is, the Func and var modifiers in curly braces are _func and _var, while the C + + part is decorated according to the C + + set.

The following section of code is often used to solve the C + + Two source code compilation form:

#ifdef __cplusplusextern "C"{#endifvoid *memset(void *, int, size_t);#ifdef __cplusplus}#endif

If you are currently participating in the compilation of C + + code, Memset will be declared in the extern "C", followed by the C code for symbolic decoration, and if it is C code, declare it directly. (The C language does not support extern "C", which is defined by the C + + compiler by default and is defined by default if it is compiled by the C + + compiler). __cplusplus )。 This code is used in almost all of the system header files.

2015-12-04 Debug Record Zhang Bongyi

Solution to call C function module in "VS Development" MFC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.