Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to support c++ 20 module name mangling when Interfacing to C++? #20641

Open
pengybhnu opened this issue Jan 6, 2025 · 2 comments
Open

How to support c++ 20 module name mangling when Interfacing to C++? #20641

pengybhnu opened this issue Jan 6, 2025 · 2 comments
Labels
Feature:extern (C/C++/Obj-C) interfacing to C, C++, Objective-C code

Comments

@pengybhnu
Copy link

pengybhnu commented Jan 6, 2025

I reference cpp_interface,calling C++ Global Functions from D but link error when compile c++ source in c++20 module syntax as library.
app.d source file content

module app ;
import std.stdio;
extern (C++,dmd) int foo(int i, int j, int k);

void main()
{
    auto x = foo(1, 2, 3);
    writeln(x);
}

compile foo.cppm with cmake , generate libfoo.so

add_library(geo SHARED)
target_sources(geo PUBLIC
    FILE_SET CXX_MODULES FILES 
    ${CMAKE_SOURCE_DIR}/src/geo.cppm
)
// foo.cppm
module;
#include <iostream>
export module Test;
using namespace std;
export namespace detail {

int foo(int i, int j, int k) {
  cout << "i = " << i << endl;
  cout << "j = " << j << endl;
  cout << "k = " << k << endl;

  return 7;
}
} // namespace detail
export namespace dmd {

int foo(int i, int j, int k) {
  cout << "i = " << i + 100 << endl;
  cout << "j = " << j + 200 << endl;
  cout << "k = " << k + 300 << endl;

  return 20;
}
} // namespace detail

when use dub build to link ,gave the error

undefined reference to `dmd::foo(int, int, int)'
clang-18: error: linker command failed with exit code 1 (use -v to see invocation)
Error: undefined reference to `dmd::foo(int, int, int)`
       perhaps a library needs to be added with the `-L` flag or `pragma(lib, ...)`

dump name symbol

╰─ llvm-nm-18 -C build/libgeo.so |grep foo
0000000000001260 T dmd::foo@Test(int, int, int)
00000000000011b0 T detail::foo@Test(int, int, int)
@pengybhnu pengybhnu changed the title support c++ 20 module name mangling How to support c++ 20 module name mangling when Interfacing to C++? Jan 6, 2025
@thewilsonator thewilsonator added the Feature:extern (C/C++/Obj-C) interfacing to C, C++, Objective-C code label Jan 6, 2025
@Lars-Kristiansen
Copy link

As a workaround you might try to see if pragma(mangle, <the_right_thing_as_a_string>) is of any help. But clearly even if it appears that this works, that does not scale, i.e not a good general solution.

@pengybhnu
Copy link
Author

pengybhnu commented Jan 8, 2025

As a workaround you might try to see if pragma(mangle, <the_right_thing_as_a_string>) is of any help. But clearly even if it appears that this works, that does not scale, i.e not a good general solution.

thanks, it works! may be better if extern (C++,<namespace>,<c++ module name>) syntax supported by compiler

  pragma(mangle, "_ZN3dmdW4Test3fooEiii")
  extern (C++) int foo(int i, int j, int k);
llvm-nm-18 build/libgeo.so |grep foo                     
00000000000012e0 T _ZN3dmdW4Test3fooEiii
0000000000001230 T _ZN6detailW4Test3fooEiii

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:extern (C/C++/Obj-C) interfacing to C, C++, Objective-C code
Projects
None yet
Development

No branches or pull requests

3 participants