chapel-lang / chapel

a Productive Parallel Programming Language
https://chapel-lang.org
Other
1.76k stars 413 forks source link

Compiling a `--library` with GPU support (or C++) should use C linkage in the generated header #25222

Open e-kayrakli opened 2 weeks ago

e-kayrakli commented 2 weeks ago

Compiling with --library results in generating a header file among others. If you want to use this mode with GPU support, you'd typically want to compile your application in C++ mode. But you can't use Chapel functions with C++ linkage. So, the header we generate in this mode must have:

#ifdef __cplusplus
extern "C" {
#endif

and the corresponding closing brace at the end.

Without it, we get undefined reference for each Chapel-based symbol.

Note that this is probably a preliminary requirement for calling Chapel from C++ without GPU support as well.

mppf commented 2 weeks ago

I'd probably head in the direction you describe.

However I'd like to point out an alternative, which is to put the extern "C" wherever the header file is included, e.g. for users to write:

extern "C" {
  #include "my-chapel-generated-header.h"
}

The reasons that I think putting this inside the header (as you describe) is probably better are:

  1. I think this is relatively common for libraries supporting both C and C++
  2. Since we generate the header, we can generate this boiler-plate easily enough and don't need to ask users to do it
  3. Less for users to worry about if they don't need to write the extern "C".
e-kayrakli commented 2 weeks ago

That approach doesn't work -- at least for the case I am working with. The header we generate also includes things like stdchpl.h, which down the line could include things like atomics and some GPU support code etc where we need C++ linkage. So, doing what you suggest results in other issues coming from the Chapel runtime.

There could be a way out of it; this was my experience after a quick attempt.