Sure, this is what I did. I'm working on Windows with MinGW.

sayhello.go:

// package name: hello
package main

import "C"
import "fmt"

//export SayHello
func SayHello() {
fmt.Printf("Hello!\n")
fmt.Scanln()
fmt.Printf("Goodbye\n")
}

func main() {
// We need the main function to make possible
// CGO compiler to compile the package as C shared library
}

Then hello.c:

#include "hello.h"
#include <stdio.h>

void __stdcall __declspec(dllexport) hello()
{
   SayHello();
}

int main() {
  printf("Hello from C\n");
  SayHello();
  return 0;
}

I build with the following commands:

$ go build -gcflags "-N -l" -buildmode=c-archive -o hello.a sayhello.go
$ gcc -o hello.dll -shared hello.c hello.a -lWinMM -lntdll -lWS2_32

Then I have a dload.cpp which will load the dll and call hello() (most code 
scraped from the Internet)

#include <windows.h>
#include <iostream>

typedef void (__stdcall *f_funci)();

int main()
{
   HINSTANCE hGetProcIDDLL = LoadLibrary("hello.dll");

   if (!hGetProcIDDLL) {
      std::cout << "could not load the dynamic library" << std::endl;
      return EXIT_FAILURE;
   }

  //  resolve function address here
  f_funci funci = (f_funci)GetProcAddress(hGetProcIDDLL, "hello");
  if (!funci) {
    std::cout << "could not locate the function" << std::endl;
    return EXIT_FAILURE;
  }

  funci();
  // std::cout << "sayhello() returned " << << std::endl;

  return EXIT_SUCCESS;
}

I build it with g++ -o dload dload.cpp, and it runs properly: load the 
.dll, run hello() that says "Hello", waits for the user to hit Enter, and 
says "Goodbye".

Now, if I debug with gdb:

(gdb) *break main*
Breakpoint 1 at 0x4015bd
(gdb) *run*
Starting program: C:\Users\amellan\src\ngm\examples\sharedlib\dload.exe
[New Thread 17956.0x2454]
[New Thread 17956.0x2be0]
[New Thread 17956.0x3ec0]
[New Thread 17956.0x4420]

Thread 1 hit Breakpoint 1, 0x00000000004015bd in main ()
(gdb) *s*
Single stepping until exit from function main,
which has no line number information.
[New Thread 17956.0x1c1c]
[New Thread 17956.0x1480]
[New Thread 17956.0x41d0]
[New Thread 17956.0x46a0]
[New Thread 17956.0x3074]
Hello!

Goodbye
__tmainCRTStartup ()
    at 
C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:336
336     C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c: 
No such file or directory.
(gdb)

I hit "Enter" to continue through the Go code and get "Goodbye".

Trying to attach with dlv gives:

$ *dlv attach 18336*
could not get Go symbols no runtime.pclntab symbol found

Which kind of makes sense, since this is not a pure Go executable.

I don't really care about debugging the C code, which will be just a shim 
layer, I would like to debug Go. What am I missing?

-- alain.


On Tuesday, June 6, 2017 at 7:05:00 PM UTC-7, brainman wrote:
>
> It would be useful if you provide complete instructions of what you did, 
> so we could at least be able to reproduce it here. Including complete 
> source code and description of environment and tools you use. Thank you.
>
> Alex
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to