Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of mixed programming between C++ and C #

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what is the method of mixed programming of C++ and C". In the daily operation, I believe that many people have doubts about the method of mixed programming of C++ and C. the editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the method of mixed programming of C++ and C". Next, please follow the editor to study!

Recently, I often see it in the header file.

# ifdef _ cplusplusextern "C" {# endif.#ifdef _ cplusplus} # endif1. The origin of this way of writing

C++ appears later than C, and C++ code will make better use of existing achievements if it can call C code, but in fact C++ code cannot directly call C code. This is because the function name generated by C++ compiler when compiling .cpp file is different from that generated by C compiler when compiling .c file.

In order to support overloading, C++ 's compiler modifies the original function name after compilation, such as

Test (int I) and

Test (int I, int j)

After compilation, these two functions may be modified by the C++ compiler to:

_ ZDtesti

_ ZDtestii

This style

But the C compiler will not change the function name, so the problem arises. If a header file that declares the C function is included in a C++ code, it is likely that the C function name declared in the header file will be modified after compilation. In this way, the problem of not finding the function name will occur when using this C function in C++ code. In fact, this function exists in C code, but the C++ compiler wishfully modified the function name. So how to solve it?

two。 Solution.

Very simply, explicitly tell C++ compiler that this code is a function compiled in C language, so you should not convert the function name to C++ format.

Extern "C" {int socket_send (); / / explicitly tells C++ compiler that this is a function compiled in C language}

In this way, when the C++ compiler executes this code, it recognizes the extern "C" keyword and compiles the code in parentheses in the way of the C compiler.

3. The resulting problems

Although there is no problem in C++ compilation, but if a .c file to include the header file, there will be a problem, because the extern "C" is not a C language keyword, so the .c file can not contain the header file. How can you make the .cpp file contain the header file and the .c file contain the header file, so the following is written:

Using conditional compilation, if it is judged to be a C++ compiler, bring extern "C", if it is a C compiler without extern "C", thus, the problem is properly solved. It is important to note that _ _ cplusplus is a built-in macro in the C++ compiler.

At this point, the study of "what is the method of C++ and C mixed programming" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report