Skip to content

Conversation

@MisterDA
Copy link
Contributor

@MisterDA MisterDA commented Nov 5, 2024

C++ name mangling applies to symbols: variable and function names. The rule of thumb is to enclose public symbols declarations in blocks, to avoid C++ linkers look for C++ symbol names instead of C.

#ifdef __cplusplus
extern "C" {
#endif

/* symbols go here */

#ifdef __cplusplus
}
#endif

Symbols protected by CAML_INTERNALS blocks need not to be covered. I've sometimes reordered a few functions to merge two sections protected by CAML_INTERNALS.

Headers that contain definitions incompatible with C++, such as _Atomic, also need protection.

There shouldn't be anything but comments before header guards to allow for multiple include optimization.

Fix #13541. Close #11557.

Copy link
Contributor

@NickBarnes NickBarnes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me; consistency is a good thing.

@MisterDA MisterDA force-pushed the headers-cpp-extern-c-compat branch 3 times, most recently from 96a7d40 to 0a67d38 Compare November 7, 2024 17:30
C++ name mangling applies to symbols: variable and function names. The
rule of thumb is to enclose public symbols declarations in blocks:

    #ifdef __cplusplus
    extern "C" {
    #endif

    /* symbols go here */

    #ifdef __cplusplus
    }
    #endif

Symbols protected by CAML_INTERNALS blocks need not to be covered.

Headers that contain definitions incompatible with C++, such as
_Atomic, also need protection.
@MisterDA MisterDA force-pushed the headers-cpp-extern-c-compat branch from 0a67d38 to 9ba790d Compare November 12, 2024 17:53
@MisterDA
Copy link
Contributor Author

MisterDA commented Nov 12, 2024

I've added a test that checks whether C++ stubs can be compiled and linked. I think it replaces and should close #11557. Thanks @kit-ty-kate for the tip!

This should be considered for 5.3 and 5.2.

- Test whether OCaml C headers are also valid in C++;
- Test whether C++ files can be linked with the runtime. Symbols that
  are not covered by CAML_INTERNALS need to have C linkage (under
  extern "C" { ... }). There are too many to check exhaustively, so
  use a dummy stub for now.

Co-authored-by: Kate <kit-ty-kate@outlook.com>
@MisterDA MisterDA force-pushed the headers-cpp-extern-c-compat branch from 9ba790d to 1addeb9 Compare November 12, 2024 22:57
@ghost
Copy link

ghost commented Nov 13, 2024

Mangling of variable names only matters when namespaces are used, which is obviously not the case in OCaml. Therefore several of the added guards are unnecessary, e.g. runtime/caml/address_class.h doesn't need any.

@dra27 dra27 added this to the 5.3.0 milestone Nov 13, 2024
@MisterDA
Copy link
Contributor Author

Therefore several of the added guards are unnecessary, e.g. runtime/caml/address_class.h doesn't need any.

Is that a strong blocker? Can we keep them for future use, or should I remove them?

@ghost
Copy link

ghost commented Nov 14, 2024

No, it's not a blocker.

@Octachron
Copy link
Member

@NickBarnes , did you have the time to finish your review? If possible I would like to include this fix in the next beta for 5.3 (that should happen next week hopefully).

@MisterDA
Copy link
Contributor Author

I'm tempted to add the C++ guards to all headers, unconditionally, even encompassing CAML_INTERNALS sections…

Copy link
Contributor

@NickBarnes NickBarnes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Having a test is great. As noted in your comment, it would be nice if the test's #includes were auto-generated, but that's a problem for another time.

@NickBarnes
Copy link
Contributor

@Octachron I apologise for the delay.

@NickBarnes NickBarnes merged commit b0b5f92 into ocaml:trunk Nov 26, 2024
16 checks passed
NickBarnes added a commit to NickBarnes/ocaml that referenced this pull request Nov 26, 2024
Audit the installed headers for C++ compatibility

(cherry picked from commit b0b5f92)
@NickBarnes
Copy link
Contributor

Merged and cherry-picked to 5.3.

@Octachron
Copy link
Member

I agree that cherry-picking 38b9c9e to 5.3 is a good idea too to avoid diverging ocamltest tests.

@NickBarnes
Copy link
Contributor

Done under @dra27's close supervision ....

@MisterDA MisterDA deleted the headers-cpp-extern-c-compat branch November 26, 2024 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[5.2 & 5.3] Compiling C++ code using the OCaml C API results in name-mangled caml_state on Cygwin

4 participants