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: error while running C application which imports methods from the DLL created using c-shared in go #32101

Open
Shashi-S opened this issue May 17, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@Shashi-S
Copy link

commented May 17, 2019

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

$ go version
1.11

Does this issue reproduce with the latest release?

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

go env Output
$ go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\cbairy\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:\Code\Go
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
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
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\xyz\AppData\Local\Temp\go-build413893962=/tmp/go-build -gno-record-gcc-switches

What did you do?

Created a simple C console application which imports functions exposed in the DLL and the DLL was created using c-shared. The go DLL depends on other DLL which was created using C++. The dependent DLL are placed in the correct path so console application is able to locate them while loading. "The application was unable to to start correctly(0x0000142)" error is seen while launching console application.

#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) char* testing1(char* input);
__declspec(dllexport) char* testing2(char* input);

#ifdef __cplusplus
}
#endif

check.cpp
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define DllExport __declspec(dllexport)

extern "C" __declspec(dllexport) char* testing1(char* input){
    return input;
}
extern "C" __declspec(dllexport) char* testing2(char* input){
    return input;
}

int main(int argc, char* argv[])
{
    char* result = testing1("testing ");
    std::cout<<"Result : "<< result<<std::endl;
}

Go code for creating DLL which has dependency on above created Check.dll

package main
/*
#cgo LDFLAGS: -lCheck
#include <check.h>
#include <stdlib.h>
#include <string.h>
*/
import "C"
import "fmt"

//export AddTwoNums
func AddTwoNums(numone int, numtwo int) int {
    return numone + numtwo
}

//export Validate
func Validate(args *C.char) *C.char {
    str := C.GoString(args)
    str = str + "-" + str
    return C.CString(str)
}

func importFromClib1() {
    str1 := "Testing1"
    cstr := C.CString(str1)
    result := C.testing1(cstr)
    fmt.Println("Testing1 result : ", C.GoString(result))
}

func importFromClib2() {
    str1 := "Testing2"
    cstr := C.CString(str1)
    result := C.testing2(cstr)
    fmt.Println("Testing2 result : ", C.GoString(result))
}

func main() {
} 

Note: The same sequence works fine on Mac with dylib.

What did you expect to see?

The C console application should run fine without any exception since the application just calls the method from DLL created from go

What did you see instead?

The application was unable to to start correctly(0x0000142)" error is seen while launching C console application.

@FiloSottile FiloSottile changed the title Error while running C application which imports methods from the DLL created using c-shared in go cmd/cgo: error while running C application which imports methods from the DLL created using c-shared in go May 17, 2019

@FiloSottile FiloSottile added this to the Go1.13 milestone May 17, 2019

@FiloSottile

This comment has been minimized.

Copy link
Member

commented May 17, 2019

Can you check if this is still an issue with Go 1.12? Thank you!

@Shashi-S

This comment has been minimized.

Copy link
Author

commented May 19, 2019

I see the same error even with Go 1.12 .
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.