Failing to embed Julia in C++

I’m trying to embed Julia in C++ on an M2 MacBook under MacOS Sonoma with Julia 1.9.3. I’ve succeeded in compiling the same program in Windows, with Visual Studio, and Ubuntu 22.04, with g++. But on the Mac, g++ is generating an error as it proceses julia.h. I’m out of my depth here (I bought my previous Mac in 1987) and would welcome help.

Below is a log from a minimum working example taken straight from the page on embedding. It shows the test.c file, and how it compiles fine (silently) with gcc but not g++. What am I doing wrong?

% gcc --version
Apple clang version 15.0.0 (clang-1500.0.40.1)
Target: arm64-apple-darwin23.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

% cat test.c
#include <julia.h>

int main(int argc, char *argv[])
{
    jl_init();
    jl_eval_string("print(sqrt(2.0))");
    jl_atexit_hook(0);
    return 0;
}

% gcc -fPIC test.c -o test -I$JULIA_DIR/include/julia -L$JULIA_DIR/lib -Wl,-rpath,$JULIA_DIR/lib -ljulia 

% g++ -fPIC test.c -o test -I$JULIA_DIR/include/julia -L$JULIA_DIR/lib -Wl,-rpath,$JULIA_DIR/lib -ljulia
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]
In file included from test.c:1:
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:445:24: error: union member 'fptr' has a non-trivial default constructor
        _Atomic(void*) fptr;
                       ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/atomic:1749:7: note: because the function selected to construct base class of type '__atomic_base<void *>' is not trivial
    : public __atomic_base<_Tp*>
      ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/atomic:1523:36: note: because the function selected to construct field of type '__cxx_atomic_impl<void *>' is not trivial
    mutable __cxx_atomic_impl<_Tp> __a_;
                                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/atomic:1426:28: note: because base class of type 'std::__cxx_atomic_base_impl<void *>' has a user-provided default constructor
struct __cxx_atomic_impl : public _Base {
                           ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/atomic:912:5: note: declared here
    __cxx_atomic_base_impl() _NOEXCEPT : __a_value() {}
    ^
In file included from test.c:1:
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:446:33: error: union member 'fptr1' has a non-trivial default constructor
        _Atomic(jl_fptr_args_t) fptr1;
                                ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/atomic:1749:7: note: because the function selected to construct base class of type '__atomic_base<_jl_value_t *(*)(_jl_value_t *, _jl_value_t **, unsigned int)>' is not trivial
    : public __atomic_base<_Tp*>
      ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/atomic:1523:36: note: because the function selected to construct field of type '__cxx_atomic_impl<_jl_value_t *(*)(_jl_value_t *, _jl_value_t **, unsigned int)>' is not trivial
    mutable __cxx_atomic_impl<_Tp> __a_;
                                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/atomic:1426:28: note: because base class of type 'std::__cxx_atomic_base_impl<_jl_value_t *(*)(_jl_value_t *, _jl_value_t **, unsigned int)>' has a user-provided default constructor
struct __cxx_atomic_impl : public _Base {
                           ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/atomic:912:5: note: declared here
    __cxx_atomic_base_impl() _NOEXCEPT : __a_value() {}
    ^
In file included from test.c:1:
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:448:35: error: union member 'fptr3' has a non-trivial default constructor
        _Atomic(jl_fptr_sparam_t) fptr3;
                                  ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/atomic:1749:7: note: because the function selected to construct base class of type '__atomic_base<_jl_value_t *(*)(_jl_value_t *, _jl_value_t **, unsigned int, jl_svec_t *)>' is not trivial
    : public __atomic_base<_Tp*>
      ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/atomic:1523:36: note: because the function selected to construct field of type '__cxx_atomic_impl<_jl_value_t *(*)(_jl_value_t *, _jl_value_t **, unsigned int, jl_svec_t *)>' is not trivial
    mutable __cxx_atomic_impl<_Tp> __a_;
                                   ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/atomic:1426:28: note: because base class of type 'std::__cxx_atomic_base_impl<_jl_value_t *(*)(_jl_value_t *, _jl_value_t **, unsigned int, jl_svec_t *)>' has a user-provided default constructor
struct __cxx_atomic_impl : public _Base {
                           ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/atomic:912:5: note: declared here
    __cxx_atomic_base_impl() _NOEXCEPT : __a_value() {}
    ^
In file included from test.c:1:
In file included from /Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:1976:
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia_locks.h:29:20: warning: offset of on non-POD type 'jl_task_t' (aka '_jl_task_t') [-Winvalid-offsetof]
    _jl_mutex_wait(jl_current_task, lock, safepoint);
                   ^~~~~~~~~~~~~~~
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:1971:26: note: expanded from macro 'jl_current_task'
#define jl_current_task (container_of(jl_get_pgcstack(), jl_task_t, gcstack))
                         ^                                          ~~~~~~~
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:68:32: note: expanded from macro 'container_of'
    ((type *) ((char *)(ptr) - offsetof(type, member)))
                               ^              ~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/15.0.0/include/stddef.h:105:24: note: expanded from macro 'offsetof'
#define offsetof(t, d) __builtin_offsetof(t, d)
                       ^                     ~
In file included from test.c:1:
In file included from /Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:1976:
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia_locks.h:66:20: warning: offset of on non-POD type 'jl_task_t' (aka '_jl_task_t') [-Winvalid-offsetof]
    _jl_mutex_lock(jl_current_task, lock);
                   ^~~~~~~~~~~~~~~
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:1971:26: note: expanded from macro 'jl_current_task'
#define jl_current_task (container_of(jl_get_pgcstack(), jl_task_t, gcstack))
                         ^                                          ~~~~~~~
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:68:32: note: expanded from macro 'container_of'
    ((type *) ((char *)(ptr) - offsetof(type, member)))
                               ^              ~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/15.0.0/include/stddef.h:105:24: note: expanded from macro 'offsetof'
#define offsetof(t, d) __builtin_offsetof(t, d)
                       ^                     ~
In file included from test.c:1:
In file included from /Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:1976:
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia_locks.h:71:35: warning: offset of on non-POD type 'jl_task_t' (aka '_jl_task_t') [-Winvalid-offsetof]
    return _jl_mutex_trylock_nogc(jl_current_task, lock);
                                  ^~~~~~~~~~~~~~~
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:1971:26: note: expanded from macro 'jl_current_task'
#define jl_current_task (container_of(jl_get_pgcstack(), jl_task_t, gcstack))
                         ^                                          ~~~~~~~
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:68:32: note: expanded from macro 'container_of'
    ((type *) ((char *)(ptr) - offsetof(type, member)))
                               ^              ~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/15.0.0/include/stddef.h:105:24: note: expanded from macro 'offsetof'
#define offsetof(t, d) __builtin_offsetof(t, d)
                       ^                     ~
In file included from test.c:1:
In file included from /Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:1976:
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia_locks.h:76:30: warning: offset of on non-POD type 'jl_task_t' (aka '_jl_task_t') [-Winvalid-offsetof]
    return _jl_mutex_trylock(jl_current_task, lock);
                             ^~~~~~~~~~~~~~~
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:1971:26: note: expanded from macro 'jl_current_task'
#define jl_current_task (container_of(jl_get_pgcstack(), jl_task_t, gcstack))
                         ^                                          ~~~~~~~
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:68:32: note: expanded from macro 'container_of'
    ((type *) ((char *)(ptr) - offsetof(type, member)))
                               ^              ~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/15.0.0/include/stddef.h:105:24: note: expanded from macro 'offsetof'
#define offsetof(t, d) __builtin_offsetof(t, d)
                       ^                     ~
In file included from test.c:1:
In file included from /Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:1976:
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia_locks.h:81:22: warning: offset of on non-POD type 'jl_task_t' (aka '_jl_task_t') [-Winvalid-offsetof]
    _jl_mutex_unlock(jl_current_task, lock);
                     ^~~~~~~~~~~~~~~
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:1971:26: note: expanded from macro 'jl_current_task'
#define jl_current_task (container_of(jl_get_pgcstack(), jl_task_t, gcstack))
                         ^                                          ~~~~~~~
/Applications/Julia-1.9.app/Contents/Resources/julia/include/julia/julia.h:68:32: note: expanded from macro 'container_of'
    ((type *) ((char *)(ptr) - offsetof(type, member)))
                               ^              ~~~~~~
/Library/Developer/CommandLineTools/usr/lib/clang/15.0.0/include/stddef.h:105:24: note: expanded from macro 'offsetof'
#define offsetof(t, d) __builtin_offsetof(t, d)

                       ^                     ~
5 warnings and 3 errors generated.

Have you tried just embedding with C first? After that you might need to use extern C when using C++.