Question Details

No question body available.

Tags

c++ c ffi

Answers (14)

March 21, 2026 Score: 2 Rep: 1,934 Quality: Low Completeness: 0%

It should be classified as a regular technical question, not in the Best practices category. I suggest you remove your post and ask as a regular question.

March 21, 2026 Score: 2 Rep: 122,803 Quality: Low Completeness: 40%

extern "C" does not imply "give me an error if a C compiler wouldn't compile this". It is not a C++ compiler's job to judge what is or is not a valid C declaration. For one, C allows extensions and a C++ compiler cannot possibly know which ones your C compiler supports.

March 21, 2026 Score: 2 Rep: 5,503 Quality: Low Completeness: 40%

extern "C" doesnt make it a C function or a function callable by C. It makes it's linkage to be the one of C (namely, it disables name mangling of the functions)

March 21, 2026 Score: 1 Rep: 122,803 Quality: Low Completeness: 0%

The standard says nothing about name mangling. It does say that the C linkage is "to the C programming language", whatever that means.

March 21, 2026 Score: 0 Rep: 8,172 Quality: Low Completeness: 0%

what devilry is this? do you mean "Troubleshooting/debugging" or "General advice/other"?

March 21, 2026 Score: 0 Rep: 43,707 Quality: Low Completeness: 0%

If there's a platform for which references to forward declared structures don't compile to pointers I want to know about it.

March 21, 2026 Score: 0 Rep: 611,689 Quality: Low Completeness: 0%

"Troubleshooting / Debugging" is the traditional Q&A format that StackOverflow was designed for. The others are more recent types added for open-ended discussions.

March 21, 2026 Score: 0 Rep: 35,535 Quality: Low Completeness: 10%

is it safe to use references in extern "C"

Very simply, no.

If you scour the entire C++ language specification (thousands of pages), nowhere will it mention how C++ references are implemented, only how to use references. There isn't even a guarantee that a C++ reference is implemented the same way across compilers or compiler versions.

For this, you probably should use pointers, not references.

March 21, 2026 Score: 0 Rep: 8,172 Quality: Low Completeness: 0%

ok thanks. Though I'm no longer allowed to delete it

March 21, 2026 Score: 0 Rep: 49,051 Quality: Low Completeness: 30%

How extern "C" works is implementation dependent. Try the documentation for the compiler you are using.

March 21, 2026 Score: 0 Rep: 44,774 Quality: Low Completeness: 70%

extern "C" is implementation-defined (https://eel.is/c++draft/dcl.link#10), so you have to look at the implementation's documentation. Many implementations are documented to handling references as pointers (https://itanium-cxx-abi.github.io/cxx-abi/abi.html#reference-parameters), or that they can be inferred to have, in C parlance, 'compatible types' (thus would be completely legal according to the C standard)

March 21, 2026 Score: 0 Rep: 1,101 Quality: Low Completeness: 40%

An opaque type Bar isn't FFI-usable. Make it POD and pass a pointer instead of a reference. It is safe to use references in extern "C", but for your use case, it shouldn't be done.

March 21, 2026 Score: 0 Rep: 8,172 Quality: Low Completeness: 50%

An opaque type Bar isn't FFI-usable

Can you elaborate? My FFI has worked so far for the scheme build. I don't use this data in the other language, I just pass it back to C++.

Make it POD and pass a pointer instead of a reference

Do you mean make my own POD version of quz::Bar? I wouldn't be able to do that for the library I'm wrapping. It's enormous.

It is safe to use references in extern "C", but for your use case, it shouldn't be done.

What is distinct about my use case?

March 21, 2026 Score: 0 Rep: 8,172 Quality: Low Completeness: 30%

OK, so do I understand right it's up to extern "C" to sort the linkage, but it's up to me to make the function callable by C?