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: code using <iostream> header silently terminates on Windows #45468

Open
gegogi opened this issue Apr 9, 2021 · 2 comments
Open

cmd/cgo: code using <iostream> header silently terminates on Windows #45468

gegogi opened this issue Apr 9, 2021 · 2 comments

Comments

@gegogi
Copy link

@gegogi gegogi commented Apr 9, 2021

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

$ go version
go version go1.16.3 windows/amd64

Does this issue reproduce with the latest release?

Yes

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

$ go env
set GOARCH=amd64
set GOOS=windows
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config

What did you do?

The following simple code silently crashes on Windows.
But executes as intended on Linux.
Strangely, if I use printf instead of cout, it runs well on Windows too.
I can mix general C++ code like std::string with printf.
So it seems the static variable such as cout, cerr inside iostream are causing this.

##### FILE: go.mod ##############################

module cgotest

##### FILE: main.go #############################

package main

import "cgotest/cpplib"

func main() {
	cpplib.Print()
}

##### FILE: cpplib/cppcode.cpp #################

#include <iostream>

extern "C" void printFromCPP()
{
	std::cout << "printFromCPP()\n";
}

##### FILE: cpplib/cpplib.go ###################

package cpplib

// void printFromCPP();
import "C"

func Print() {
	C.printFromCPP()
}
@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Apr 9, 2021

It doesn't exactly silently terminates, if you check the return code it's actually a segmentation fault.
In powershell you can print the last exit code with $LASTEXITCODE

Anyway the main issue here is that go does ASLR builds by default on windows and that sometimes messes up when linking external C/C++ code. So you actually can't do a trival go build on windows when this happens and you instead have to use go build -buildmode=exe to disable ASLR

See #35192

cc @zx2c4

Loading

@dmitshur dmitshur changed the title cgo code using <iostream> header silently terminates on Windows cmd/cgo: code using <iostream> header silently terminates on Windows Apr 9, 2021
@dmitshur dmitshur added this to the Backlog milestone Apr 9, 2021
@networkimprov
Copy link

@networkimprov networkimprov commented May 15, 2021

Loading

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