Avatar ·

What does extern "C" do in C++ and why is it needed when working with C libraries?

Hello! I started learning C++ and came across the `extern "C"` construct, which I often see in header files. For example, in libraries like SDL or when including old C functions. Here's an example I found in an article: ```cpp // my_header.h #ifdef __cplusplus extern "C" { #endif void my_c_function(int x); int another_c_function(const char* str); #ifdef __cplusplus } #endif ``` I understand that this has something to do with linking and function names, but I can't figure out the details. Specifically, I'm interested in: 1. Why can linking errors occur without `extern "C"` when using C functions in C++? 2. What is "name mangling" and how is it related to this problem? 3. Why wrap declarations in `#ifdef __cplusplus`? After all, if I'm writing in C++, then __cplusplus