• Golang与C互用


    https://jermine.vdo.pub/go/golang%E4%B8%8Ec%E4%BA%92%E7%9B%B8%E8%B0%83%E7%94%A8%E4%BB%A5%E5%8F%8A%E8%B0%83%E7%94%A8c%E7%9A%84so%E5%8A%A8%E6%80%81%E5%BA%93%E5%92%8Ca%E9%9D%99%E6%80%81%E5%BA%93/

    Golang与C互用以及调用C的so动态库和a静态库

    could not determine kind of name for C.foo

    tree
    .
    ├── sub
    │   ├── foo.go
    │   ├── foo.h
    │   └── hello.c
    └── test
        ├── main
        └── main.go
    root@ubuntu:~/go_c# tree
    .
    ├── sub
    │   ├── foo.go
    │   ├── foo.h
    │   └── hello.c
    └── test
        ├── main
        └── main.go
    
    2 directories, 5 files
    root@ubuntu:~/go_c# cat sub/foo.go 
    package   sub 
    /*
    #cgo CFLAGS: -Wall
    extern void foo();
    void __attribute__((constructor)) init(void) {
                    foo();
            }
    */
    import "C"
    
    // AlwaysFalse is here to stay false
    // (and be exported so the compiler doesn't optimize out its reference)
    func init() {
             C.init()
    }
    root@ubuntu:~/go_c# cat sub/hello.c
    #include <stdio.h>
    #include "foo.h"
    
    int count = 6;
    void foo(){
        printf("I am foo!
    ");
    }root@ubuntu:~/go_c# cat sub/foo.h
    int count;
    void foo();root@ubuntu:~/go_c# 
    root@ubuntu:~/go_c# cat test/main.go 
    package main
    import "fmt"
    import "C"
    import _  "../sub"
    func init() {
       fmt.Println("init in main.go ")
    }
    func main(){
        fmt.Println("Hello c, welcome to go!")
    }
    root@ubuntu:~/go_c/test# ./main 
    I am foo!
    I am foo!
    init in main.go 
    Hello c, welcome to go!
    root@ubuntu:~/go_c/test#
    package main
    import "fmt"
    //import "C"
    //import _  "../sub"
    func init() {
       fmt.Println("init in main.go ")
    }
    func main(){
        fmt.Println("Hello c, welcome to go!")
    }
    root@ubuntu:~/go_c/test# go build main.go
    root@ubuntu:~/go_c/test# ./main 
    init in main.go 
    Hello c, welcome to go!
    root@ubuntu:~/go_c/test# 
    root@ubuntu:~/go_c/test# cat  ../sub/foo.go
    package   sub 
    /*
    #cgo CFLAGS: -Wall
    extern void foo();
    void __attribute__((constructor)) init(void) {
                    foo();
            }
    */
    import "C"
    var AlwaysFalse bool
    
    // AlwaysFalse is here to stay false
    // (and be exported so the compiler doesn't optimize out its reference)
    func init() {
            if AlwaysFalse {
             C.init()
     }
    }
    root@ubuntu:~/go_c/test# go build main.go
    # _/root/go_c/sub
    cgo-gcc-prolog: In function ‘_cgo_b2a0b4a5114f_Cfunc_init’:
    cgo-gcc-prolog:47:33: warning: unused variable ‘_cgo_a’ [-Wunused-variable]
    root@ubuntu:~/go_c/test# ./main 
    I am foo!
    init in main.go 
    Hello c, welcome to go!
  • 相关阅读:
    怎么样通过API函数获取tooltip的内容(请高手帮忙)
    Ajax控件之Accordion控件学习
    C#中结构与类的区别
    C# ref and out 区别
    java 将PDF文件的首页提取为图片
    围住浮动元素(消除浮动)的三种方法
    myeclipse unable to install breakpoint
    C#调用C++编写的COM DLL
    高手教你玩53kf在线客服的指定客服功能
    让IIS支持Flv的详细设置方法
  • 原文地址:https://www.cnblogs.com/dream397/p/14134600.html
Copyright © 2020-2023  润新知