how does extern “C” allow C++ code in a C file?

我只是一个虾纸丫 提交于 2019-12-10 04:20:32

问题


In order to use C++ code in a C file, I read that we can just do extern "C" { (where the c++ code goes here)}, but when I try printing something out using cout, I keep getting an error because it does not recognize the library . I think I am just confused on how extern "C" allows you to use C++ code in C.


回答1:


The opposite is true. You can use extern C to add code you want to compile as C code using a C++ compiler.

Unless I'm missing something you can't compile C++ code with a C compiler.




回答2:


The extern "C" construct is a C++-specific syntax, no C compiler will understand it.

That's why you will almost always see it paired with some conditional compilation like

#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif

What extern "C" does is simply to inhibit name mangling meaning that symbols defined in a C++ source file can be used in a C program.

This allows you to have a project with mixed C and C++ sources, and the C source can call the C++ functions that have been defined as extern "C".

Very simple, and stupid, example just to show the point:

Lets say we have a C source file, compiled with a C compiler:

#include <stdio.h>

void foo(void);  // Declare function prototype, so it can be called

int main(void)
{
    printf("Calling foo...\n");
    foo();
    printf("Done\n");

    return 0;
}

Then we have a C++ source file for the foo function:

#include <iostream>

extern "C" void foo()
{
    std::cout << "Hello from foo\n";
}

The C source file is compiled with a C compiler, and the C++ source file is compiled with a C++ compiler. Then the two object files are linked together to form the executable program. Because foo was defined as extern "C" it's symbol name in the object file is not mangled, and the linker can resolve the reference from the object file created by the C compiler.

It works in the other direction as well. Because symbols in C are not mangled the C++ compiler needs to know that, and that is done by declaring the C symbols extern "C", usually in a header file using the conditional compilation as shown above. If extern "C" was not used, the C++ compiler would think that the symbols were C++ symbols, and mangle the names leading to linker problems when the linker can't find the mangled symbols.

Equally stupid example: First a C++ source file

#include <iostream>

extern "C" void bar();  // Declare function prototype as an unmangled symbol

int main()
{
    std::cout << "Calling bar...\n";
    bar();
}

Then the C source file

#include <stdio.h>

void bar(void)
{
    printf("Inside bar\n");
}



回答3:


extern "C" is a way of putting C code in C++ code. More specifically it tells the compiler to disable certain things like function overloading so that it can also turn off the name mangling. In C++ a simple function like:

int add(int a, int b) {
    return a+b;
}

Will actually get some funky name in the library to denote the parameters so that if you define another function like:

double add(double a, double b) {
    return a+b;
}

That it knows which one to call. You don't want that if you're trying to use a C library and that's what extern "C" is for. All of this being said, extern "C" does not allow C++ in a C program.




回答4:


Exported C++ symbols, as generated my the compiler, are mangled to include additional type information about the symbol to the linker.

So the following overloaded functions would be distinguishable by the linker in C++, but not C:

  • int fn();
  • int fn(int);
  • int fn(char*);
  • int fn(char*) const;
  • int fn(const char*);

The extern "C" { ... } syntax allows symbols (or references to C symbols) to be defined in C++ code without using the mangling rules. This allows such C++ code to be linked with C libraries.



来源:https://stackoverflow.com/questions/36785157/how-does-extern-c-allow-c-code-in-a-c-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!