Avatar ·

What does extern "C" do in C++ and why is it needed?

Hi everyone! I haven't been writing in C++ for long, and while working with C libraries (for example, some old graphics library), I constantly come across the `extern "C"` construct. I see it in header files, but I don't fully understand what it does. For example, I have this code: ```cpp // mylib.h #ifdef __cplusplus extern "C" { #endif void my_c_function(int x); #ifdef __cplusplus } #endif ``` Or when I include a C header file myself: ```cpp // main.cpp extern "C" { #include "clibrary.h" } ``` I've read that it has something to do with "name mangling" (naming functions in the binary), but I can't understand exactly how it works. Why does my program fail to find a function from a C library without `extern "C"` and give a linking error like