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

cmd/cgo: support linking to libraries built with clang's libc++ #48486

Closed
darkdragon-001 opened this issue Sep 20, 2021 · 3 comments
Closed

cmd/cgo: support linking to libraries built with clang's libc++ #48486

darkdragon-001 opened this issue Sep 20, 2021 · 3 comments

Comments

@darkdragon-001
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.17 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/kinexon/.cache/go-build"
GOENV="/home/kinexon/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/kinexon/go/pkg/mod"
GONOPROXY="gitlab.knx"
GONOSUMDB="gitlab.knx"
GOOS="linux"
GOPATH="/home/kinexon/go"
GOPRIVATE="gitlab.knx"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/workspaces/sn-logging/go.mod"
CGO_CFLAGS=""
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-stdlib=libc++"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-stdlib=libc++"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1273270751=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Linking to a library built with clang's libc++ by setting flag -stdlib=libc++.

What did you expect to see?

Working binary.

What did you see instead?

Linker errors in standard library.

@akupila
Copy link

akupila commented Sep 20, 2021

You didn't specify what errors you're seeing, but perhaps you're looking for -lstdc++ instead?

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For venues to ask questions, see: https://github.com/golang/go/wiki/Questions

@darkdragon-001
Copy link
Author

darkdragon-001 commented Sep 20, 2021

I am specifically looking for LLVM's libc++. With GNU's libstdc++, everything works fine. Depending on how I set the environment variables, I sometimes get an error that -stdlib=libc++ is unused or I sometimes get linker errors like

/usr/local/go/pkg/tool/linux_amd64/link: running clang failed: exit status 1
ld: error: undefined symbol: operator new(unsigned long)
>>> referenced by condition_variable.cpp.o:(std::__1::notify_all_at_thread_exit(std::__1::condition_variable&, std::__1::unique_lock<std::__1::mutex>)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by stdexcept.cpp.o:(std::logic_error::logic_error(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by stdexcept.cpp.o:(std::logic_error::logic_error(char const*)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced 264 more times

ld: error: undefined symbol: operator delete(void*)
>>> referenced by condition_variable.cpp.o:(std::__1::notify_all_at_thread_exit(std::__1::condition_variable&, std::__1::unique_lock<std::__1::mutex>)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by memory.cpp.o:(std::__1::bad_weak_ptr::~bad_weak_ptr()) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by stdexcept.cpp.o:(std::logic_error::operator=(std::logic_error const&)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced 1643 more times

ld: error: undefined symbol: std::exception::~exception()
>>> referenced by memory.cpp.o:(std::__1::bad_weak_ptr::~bad_weak_ptr()) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by memory.cpp.o:(std::__1::bad_weak_ptr::~bad_weak_ptr()) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by stdexcept.cpp.o:(std::logic_error::logic_error(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced 3 more times

ld: error: undefined symbol: vtable for __cxxabiv1::__si_class_type_info
>>> referenced by memory.cpp.o:(typeinfo for std::__1::bad_weak_ptr) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by system_error.cpp.o:(typeinfo for std::__1::system_error) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by system_error.cpp.o:(typeinfo for std::__1::__do_message) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced 119 more times
>>> the vtable symbol may be undefined because the class is missing its key function (see https://lld.llvm.org/missingkeyfunction)

ld: error: undefined symbol: typeinfo for std::exception
>>> referenced by memory.cpp.o:(typeinfo for std::__1::bad_weak_ptr) in archive /usr/lib/x86_64-linux-gnu/libc++.a

ld: error: undefined symbol: __cxa_pure_virtual
>>> referenced by memory.cpp.o:(vtable for std::__1::__shared_count) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by memory.cpp.o:(vtable for std::__1::__shared_weak_count) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by memory.cpp.o:(vtable for std::__1::__shared_weak_count) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced 24 more times

ld: error: undefined symbol: vtable for __cxxabiv1::__class_type_info
>>> referenced by memory.cpp.o:(typeinfo for std::__1::__shared_count) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by system_error.cpp.o:(typeinfo for std::__1::error_category) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by exception.cpp.o:(typeinfo for std::nested_exception) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced 35 more times
>>> the vtable symbol may be undefined because the class is missing its key function (see https://lld.llvm.org/missingkeyfunction)

ld: error: undefined symbol: vtable for __cxxabiv1::__vmi_class_type_info
>>> referenced by memory.cpp.o:(typeinfo for std::__1::__shared_weak_count) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by ios.instantiations.cpp.o:(typeinfo for std::__1::basic_istream<char, std::__1::char_traits<char> >) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by ios.instantiations.cpp.o:(typeinfo for std::__1::basic_istream<wchar_t, std::__1::char_traits<wchar_t> >) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced 47 more times
>>> the vtable symbol may be undefined because the class is missing its key function (see https://lld.llvm.org/missingkeyfunction)

ld: error: undefined symbol: __cxa_begin_catch
>>> referenced by mutex.cpp.o:(std::__1::__call_once(unsigned long volatile&, void*, void (*)(void*))) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by string.cpp.o:(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__shrink_or_extend(unsigned long)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by string.cpp.o:(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >::__shrink_or_extend(unsigned long)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced 105 more times

ld: error: undefined symbol: __cxa_rethrow
>>> referenced by mutex.cpp.o:(std::__1::__call_once(unsigned long volatile&, void*, void (*)(void*))) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by ios.cpp.o:(std::__1::ios_base::__set_badbit_and_consider_rethrow()) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by ios.cpp.o:(std::__1::ios_base::__set_failbit_and_consider_rethrow()) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced 57 more times

ld: error: undefined symbol: __cxa_end_catch
>>> referenced by mutex.cpp.o:(std::__1::__call_once(unsigned long volatile&, void*, void (*)(void*))) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by string.cpp.o:(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__shrink_or_extend(unsigned long)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by string.cpp.o:(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >::__shrink_or_extend(unsigned long)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced 195 more times

ld: error: undefined symbol: __cxa_allocate_exception
>>> referenced by new.cpp.o:(std::__throw_bad_alloc()) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by string.cpp.o:(std::__1::stoi(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long*, int)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by string.cpp.o:(std::__1::stoi(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long*, int)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced 41 more times

ld: error: undefined symbol: std::bad_alloc::bad_alloc()
>>> referenced by new.cpp.o:(std::__throw_bad_alloc()) in archive /usr/lib/x86_64-linux-gnu/libc++.a

ld: error: undefined symbol: typeinfo for std::bad_alloc
>>> referenced by new.cpp.o:(std::__throw_bad_alloc()) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by ld-temp.o
>>>               lto.tmp:(.gcc_except_table.zmq_poll+0x50)
>>> referenced by ld-temp.o
>>>               lto.tmp:(.gcc_except_table._ZN3zmq5ctx_t13create_socketEi+0x12C)
>>> referenced 1 more times

ld: error: undefined symbol: std::bad_alloc::~bad_alloc()
>>> referenced by new.cpp.o:(std::__throw_bad_alloc()) in archive /usr/lib/x86_64-linux-gnu/libc++.a

ld: error: undefined symbol: __cxa_throw
>>> referenced by new.cpp.o:(std::__throw_bad_alloc()) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by string.cpp.o:(std::__1::stoi(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long*, int)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by string.cpp.o:(std::__1::stoi(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long*, int)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced 41 more times

ld: error: undefined symbol: vtable for std::logic_error
>>> referenced by stdexcept.cpp.o:(std::logic_error::logic_error(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by stdexcept.cpp.o:(std::logic_error::logic_error(char const*)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by stdexcept.cpp.o:(std::logic_error::logic_error(std::logic_error const&)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> the vtable symbol may be undefined because the class is missing its key function (see https://lld.llvm.org/missingkeyfunction)

ld: error: undefined symbol: vtable for std::runtime_error
>>> referenced by stdexcept.cpp.o:(std::runtime_error::runtime_error(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by stdexcept.cpp.o:(std::runtime_error::runtime_error(char const*)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by stdexcept.cpp.o:(std::runtime_error::runtime_error(std::runtime_error const&)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> the vtable symbol may be undefined because the class is missing its key function (see https://lld.llvm.org/missingkeyfunction)

ld: error: undefined symbol: vtable for std::out_of_range
>>> referenced by string.cpp.o:(std::__1::stoi(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long*, int)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by string.cpp.o:(std::__1::stoi(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long*, int)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by string.cpp.o:(std::__1::stoi(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, unsigned long*, int)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced 16 more times
>>> the vtable symbol may be undefined because the class is missing its key function (see https://lld.llvm.org/missingkeyfunction)

ld: error: undefined symbol: typeinfo for std::out_of_range
>>> referenced by string.cpp.o:(std::__1::stoi(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long*, int)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by string.cpp.o:(std::__1::stoi(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long*, int)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced by string.cpp.o:(std::__1::stoi(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, unsigned long*, int)) in archive /usr/lib/x86_64-linux-gnu/libc++.a
>>> referenced 16 more times

ld: error: too many errors emitted, stopping now (use -error-limit=0 to see all errors)
clang: error: linker command failed with exit code 1 (use -v to see invocation)

when calling as follows

CGO_CXXFLAGS="-stdlib=libc++ -I/usr/lib/clang/12.0.0/include -I/usr/local/include -I/usr/include" CGO_LDFLAGS="-stdlib=libc++ -L/usr/lib/clang/12.0.0/lib -L/usr/local/lib -L/usr/lib -L/usr/lib/x86_64-linux-gnu -lpthread -lrt -lc++ -lm -lc" go test --ldflags '-extldflags "-static"' -c -a .

I think it is a bug somewhere like ENVs not passed correctly. But I will ask in your linked channels as well. Thanks for the hint!

@darkdragon-001
Copy link
Author

I was missing -lc++ -lc++abi.

@golang golang locked and limited conversation to collaborators Sep 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants