Warm tip: This article is reproduced from serverfault.com, please click

Linking two C libraries with the same function name with CGO?

发布于 2020-11-26 07:42:27

For example, I have two C libraries with the same function name:

hello1.c:

#include <stdio.h>

void hello() {
    printf("Hello from C hello1.c\n");
}

hello2.c:

#include <stdio.h>

void hello() {
    printf("Hello from C hello2.c\n");
}

How I can link it into Go executable binary? Static or dynamic doesn't matter.

I already have tried even linking it statically into separated Go plugins but it doesn't work – only the hello function from hello1.c will be called from two Go plugins:

hello1.go Go Plugin:

package main

/*
#cgo LDFLAGS: -L${SRCDIR}/lib -lhello1

void hello();
*/
import "C"
import "fmt"

func Hello() {
    fmt.Println("Hello from Go plugin hello1.go")
    fmt.Println("Call C hello1.c")
    C.hello()
}

hello2.go Go Plugin:

package main

/*
#cgo LDFLAGS: -L${SRCDIR}/lib -lhello2

void hello();
*/
import "C"
import "fmt"

func Hello() {
    fmt.Println("Hello from Go plugin hello2.go")
    fmt.Println("Call C hello2.c")
    C.hello()
}

main.go executable binary:

package main

import (
    "fmt"
    "plugin"
)

func main() {
    p1, err := plugin.Open("hello1/hello1.so")
    if err != nil {
        panic(err)
    }
    fmt.Println("Load Go plugin hello1.so")

    p2, err := plugin.Open("hello2/hello2.so")
    if err != nil {
        panic(err)
    }
    fmt.Println("Load Go plugin hello2.so")

    h1, err := p1.Lookup("Hello")
    if err != nil {
        panic(err)
    }

    h2, err := p2.Lookup("Hello")
    if err != nil {
        panic(err)
    }

    fmt.Println("Call Go plugin hello1.go")
    h1.(func())()
    fmt.Println("\nCall Go plugin hello2.go")
    h2.(func())()
}

Output after running main executable binary:

./main
Load Go plugin hello1.so
Load Go plugin hello2.so

Call Go plugin hello1.go
Hello from Go plugin hello1.go
Call C hello1.c
Hello from C hello1.c

Call Go plugin hello2.go
Hello from Go plugin hello2.go
Call C hello2.c
Hello from C hello1.c <=== here hello1.c, but I expected hello2.c

Update:

I have done the issue to golang to get an answer: https://github.com/golang/go/issues/42854

Questioner
DsXack
Viewed
1
DsXack 2020-11-28 22:40:34

I got the answer to my question in github issue https://github.com/golang/go/issues/42854

When I compile C libraries I should use -fvisibility=hidden gcc flag.