The correct solution
Since #include is a simple text replacement by the preprocessor, we can put the extern "C" declaration in our C++ code, where it belongs:
//main.cpp
extern "C" {
#include "foo.h"
}
int main() {
foo(22);
}
This way, everything inside the header, including the indirectly included declarations in other C headers, appear inside the extern "C" declaration.