Why does the C ++ program call C compiler compiler need to add extern "C" before calling

xiaoxiao2021-03-06  113

In the nearest interview, there are countless corporate written test topics from Lin Rui's "High Quality C Programming Guide", which has a topic like this, not necessarily, but the problem is almost, my answer is C is different from the C function call rules. The following is the answer:

What should I do if the C program wants to call the C function that has been compiled? Suppose a C function declaration is as follows: Void foo (int X, int y); this function is compiled by the C compiler to _foo in the library, and the C compiler produces a name like _foo_int_int. Used to support function overload and type secure connection. Due to the different names, the C program cannot directly call C functions. C provides a C-connection exchange specifying symbol EXTERN "C" to solve this problem. For example: extern "c" {void foo (int x, int y); // other function} or written into externaln "c" {# include "myHeader.h" // other C head file} This tells the C compilation translator The function foo is a C connection, should go to the library to find name _foo instead of find _foo_int_int. The C compiler developer has made Extern "C" on the header file of the C standard library, so we can directly reference these header files directly with #include.

转载请注明原文地址:https://www.9cbs.com/read-124621.html

New Post(0)