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
Milestone

Comments

@Keithcat1
Copy link

@Keithcat1 Keithcat1 commented Aug 5, 2020

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

@AlexRouSg AlexRouSg commented Aug 5, 2020

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

@ianlancetaylor ianlancetaylor commented Aug 5, 2020

@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 this to the Backlog milestone Aug 7, 2020
@Keithcat1
Copy link
Author

@Keithcat1 Keithcat1 commented Aug 8, 2020

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

@AlexRouSg AlexRouSg commented Aug 8, 2020

@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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.