Skip to content

cl:compile with clang++#1147

Merged
xushiwei merged 13 commits intogoplus:mainfrom
luoliwoshang:c++
Jun 12, 2025
Merged

cl:compile with clang++#1147
xushiwei merged 13 commits intogoplus:mainfrom
luoliwoshang:c++

Conversation

@luoliwoshang
Copy link
Member

@luoliwoshang luoliwoshang commented Jun 11, 2025

fixed #1146

with the case for #1146,can run normally

git clone https://github.com/goplus/lib --branch v0.2.0
cd ./lib/cpp/std/_demo/cpphello
llgo run .
git:(main) llgo run .
Hello world
Hello world
12

todo: update https://github.com/goplus/lib/blob/v0.2.0/py/_pyg/module.c to use extern "C" to ensure symbol name

ld64.lld: error: undefined symbol: llgoLoadPyModSyms
>>> referenced by /var/folders/y6/nj790rtn62lfktb1sh__79hc0000gn/T/ba2c279d93bbfecee748b353857265db8181469610e61cab9f43750ad4f70fa9-d-871d82.o:(symbol github.com/goplus/llgo/_xtool/pydump.init+0x5c)
>>> did you mean to declare llgoLoadPyModSyms(PyObject*, ...) as extern "C"?
>>> defined in: /var/folders/y6/nj790rtn62lfktb1sh__79hc0000gn/T/02808894111fc2794d2934f95173b15acc56cff66337b2cddb449c0b58c9a6ba-dmodule-c91f13.o

use the clang++ to compiler,when compile with use a wrap .c's file like runtime/internal/clite/bitcast/bitcast.go ,will got follow

ld64.lld: error: undefined symbol: llgoToFloat64
>>> referenced by /var/folders/5j/sgtxqmbn1hbdqtgx5kkp6y700000gn/T/a06741472c6f22e6e842173a0de2302c1100e20e1ae0d3b2016a2e50d15ad40a-d-333cbb.o:(symbol github.com/goplus/llgo/runtime/internal/runtime.printanycustomtype+0x508)
>>> did you mean to declare llgoToFloat64(long long) as extern "C"?
>>> defined in: /var/folders/5j/sgtxqmbn1hbdqtgx5kkp6y700000gn/T/462e5a14b76a549cc31bd8e6feea9ffe78e028b1106bfb2bb15a8e80714fc2ee-dcast-18caea.o

ld64.lld: error: undefined symbol: llgoToFloat32
>>> referenced by /var/folders/5j/sgtxqmbn1hbdqtgx5kkp6y700000gn/T/a06741472c6f22e6e842173a0de2302c1100e20e1ae0d3b2016a2e50d15ad40a-d-333cbb.o:(symbol github.com/goplus/llgo/runtime/internal/runtime.printanycustomtype+0x404)
>>> did you mean to declare llgoToFloat32(int) as extern "C"?
>>> defined in: /var/folders/5j/sgtxqmbn1hbdqtgx5kkp6y700000gn/T/462e5a14b76a549cc31bd8e6feea9ffe78e028b1106bfb2bb15a8e80714fc2ee-dcast-18caea.o
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
panic: exit status 1

this question is when use clang++ to compile .c file,the symbol name is mangled.

/opt/homebrew/Cellar/llvm@19/19.1.7/bin/clang++ -emit-llvm -S -o /Users/zhangzhiyang/Library/Caches/go-build/46/462e5a14b76a549cc31bd8e6feea9ffe78e028b1106bfb2bb15a8e80714fc2ee-dcast.c.ll -c /Users/zhangzhiyang/Documents/Code/goplus/llgo/runtime/internal/clite/bitcast/_cast/cast.c

the correspoding .ll like this , the function name is mangled

; Function Attrs: mustprogress noinline nounwind optnone ssp uwtable(sync)
define noundef double @_Z13llgoToFloat64x(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca %union.castUnion, align 8
  store i64 %0, ptr %2, align 8
  %4 = load i64, ptr %2, align 8
  store i64 %4, ptr %3, align 8
  %5 = load double, ptr %3, align 8
  ret double %5
}

so we need make these c file to cpp file ,and with a extern "C" to make symbol correct with .c to .cpp
cast.c -> cast.cpp

+ extern "C" { 

double llgoToFloat64(long long v)
{
    castUnion k;
    k.f = v;
    return k.v;
}

+ }
  • other solution is when the source file is with .c,we make the clang++ use -x c mode to compile it to ir,we can get a no mangled symbol.
/opt/homebrew/Cellar/llvm@19/19.1.7/bin/clang++ -x c -emit-llvm -S -o ./test.ll -c /Users/zhangzhiyang/Documents/Code/goplus/llgo/runtime/internal/clite/bitcast/_cast/cast.c
; ModuleID = '/Users/zhangzhiyang/Documents/Code/goplus/llgo/runtime/internal/clite/bitcast/_cast/cast.c'
source_filename = "/Users/zhangzhiyang/Documents/Code/goplus/llgo/runtime/internal/clite/bitcast/_cast/cast.c"
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128-Fn32"
target triple = "arm64-apple-macosx15.0.0"

%union.castUnion = type { double }

; Function Attrs: noinline nounwind optnone ssp uwtable(sync)
define double @llgoToFloat64(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca %union.castUnion, align 8
  store i64 %0, ptr %2, align 8
  %4 = load i64, ptr %2, align 8
  store i64 %4, ptr %3, align 8
  %5 = load double, ptr %3, align 8
  ret double %5
}

; Function Attrs: noinline nounwind optnone ssp uwtable(sync)
define float @llgoToFloat32(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca %union.castUnion, align 8
  store i32 %0, ptr %2, align 4
  %4 = load i32, ptr %2, align 4
  %5 = sext i32 %4 to i64
  store i64 %5, ptr %3, align 8
  %6 = load float, ptr %3, align 8
  ret float %6
}

@luoliwoshang luoliwoshang marked this pull request as draft June 11, 2025 09:01
Comment on lines 11 to 13
const (
LLGoFiles = "_wrap/debug.c"
LLGoFiles = "$(llvm-config --cflags): _wrap/debug.cpp"
)
Copy link
Member Author

Choose a reason for hiding this comment

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

libunwind.h will not found ,so when compile linux,to add $(llvm-config --cflags) for compile.

clang++ -emit-llvm -S -o /home/runner/.cache/go-build/02/0293ed8bec74bf22083a36df5d607e76abe301dd598dc3f1ed1c5630e5b817b8-ddebug.cpp.ll -c /home/runner/work/llgo/llgo/runtime/internal/clite/debug/_wrap/debug.cpp
/home/runner/work/llgo/llgo/runtime/internal/clite/debug/_wrap/debug.cpp:10:10: fatal error: 'libunwind.h' file not found

@luoliwoshang luoliwoshang marked this pull request as ready for review June 11, 2025 13:16
@luoliwoshang luoliwoshang marked this pull request as draft June 12, 2025 03:24
@luoliwoshang luoliwoshang force-pushed the c++ branch 2 times, most recently from 84f64bb to 6ed092e Compare June 12, 2025 03:55
@codecov
Copy link

codecov bot commented Jun 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.64%. Comparing base (47c119a) to head (93197e7).
Report is 18 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1147   +/-   ##
=======================================
  Coverage   86.64%   86.64%           
=======================================
  Files          29       29           
  Lines        7343     7343           
=======================================
  Hits         6362     6362           
  Misses        914      914           
  Partials       67       67           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@luoliwoshang luoliwoshang marked this pull request as ready for review June 12, 2025 04:20
@xushiwei xushiwei merged commit c863daf into goplus:main Jun 12, 2025
34 checks passed
luoliwoshang added a commit to luoliwoshang/llcppg that referenced this pull request Jun 12, 2025
MeteorsLiu pushed a commit to goplus/llcppg that referenced this pull request Jun 12, 2025
* deps:work with goplus/llgo#1147

* ci:libunwind & libc++ in linux

* ci:main llgo 26aaa65c1d58449fc5de1d7004df92c9e45a6136

* revert testcase

* revert testcase
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.

panic:test link args ' c++' failed when use lib/cpp/std in linux

2 participants