0

I am trying to debug a very simple cgo application in visual studio code and i am getting an error. When i go run main.go the application runs fine with no issues. But when i try to use the debugger in visual studio code, i get the following error:

# prolink007/simple-cgo
C:\Users\proli\AppData\Local\Temp\go-build964876546\b001\_x003.o: In function `Hello':
./hello.c:4: multiple definition of `Hello'
C:\Users\proli\AppData\Local\Temp\go-build964876546\b001\_x002.o:d:/projects/simple-cgo/main.go:6: first defined here
collect2.exe: error: ld returned 1 exit status
exit status 2
Process exiting with code: 1

Here is my main.go

package main

/*
  #include "hello.c"
*/
import "C"
import (
    "errors"
    "log"
)

func main() {
    err := HelloWorld()
    if err != nil {
        log.Fatal(err)
    }
}

func HelloWorld() error {
    _, err := C.Hello()
    if err != nil {
        return errors.New("error calling Hello function: " + err.Error())
    }

    return nil
}

Here is my hello.c

//hello.c
#include <stdio.h>

void Hello(){
    printf("Hello world\n");
}

Here is my launch.json.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${fileDirname}",
            "env": {},
            "args": []
        }
    ]
}

How can i debug this application in visual studio code or is there a different means to debug this?

1 Answer 1

0

I asked the same question on github and got an answer that solved my problem.

I have reproducted it on linux.

Can you try go build -gcflags="all=-N -l" ./ with

//#cgo LDFLAGS: -Wl,--allow-multiple-definition 
import "C" 

as described in golang/go#16513 ?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.