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: fix C linker problems when linking C++ static libraries #40594

Open
Keithcat1 opened this issue Aug 5, 2020 · 4 comments
Open

cmd/cgo: fix C linker problems when linking C++ static libraries #40594

Keithcat1 opened this issue Aug 5, 2020 · 4 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@Keithcat1
Copy link

Currently, if I link a static library containing C++ files, Go can't tell the difference and uses the C linker for example gcc.exe (not g++.exe), which causes the standard C++ libraries to not be linked.
I can think of 2 ways to fix this.
Either add a "#cgo: c++" directive that tells Go that this is a C++ package, or just always use the C++ linker.

@AlexRouSg
Copy link
Contributor

CGO only allows calling/using C code within Go, not C++. To use C++ code you need to create a C wraper, you can do that by adding a .cpp and a .h file with the C API into the package.

@ianlancetaylor I can't find any obvious place where this is explicitly pointed out, maybe it should?

@ianlancetaylor ianlancetaylor changed the title CGO: Fix C linker problems when linking C++ static libraries cmd/cgo: fix C linker problems when linking C++ static libraries Aug 5, 2020
@ianlancetaylor
Copy link
Contributor

@AlexRouSg There is a hint at https://golang.org/cmd/go/#hdr-Calling_between_Go_and_C.

@Keithcat1 Perhaps you could use something like CC=g++. But I'm not quite picturing the problem. Do you have an archive that contains C++ file but also provide a pure C interface, and you are using cgo to call the pure C interface?

@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 7, 2020
@toothrot toothrot added this to the Backlog milestone Aug 7, 2020
@Keithcat1
Copy link
Author

Yes, my situation is that I'm statically linking C++ libraries that come with a C API, then using the C API to call into that package.
Actually setting CC=g++ could work, but I'd prefer a solution where I don't have to set something special every time I compile a C++ package using CGO. Plus, if that works, there's no reason to not use g++ by default anyway.

@AlexRouSg
Copy link
Contributor

@Keithcat1 Try adding a empty .cpp file maybe with the include lines.

If that doesn't help, please provide more info by filling out the issue template that was shown when making a new issue. And provide the actual linker errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Status: Triage Backlog
Development

No branches or pull requests

5 participants