go - 如何将 Golang 包链接到 Windows x86-64 上的现有 C 项目(使用 Go from C)

标签 go

如何使用 Golang 从 C 代码生成的 libgolang.a 来构建 C 可执行文件:test.exe:

这些命令在 Ubuntu x86-64 中生成可执行二进制文件“test”并且工作正常(但在 Windows x86-64 中不行):

go build -buildmode c-archive -o libgolang.a
gcc -o test _main.c libgolang.a -lpthread

与:
这是 main.go 文件:

package main

import "C"
import "fmt"

//export Add
func Add(a, b uint64) uint64 {
    return a + b
}

func main() {
    fmt.Println("Hi")
}

这是_main.c文件:

#include <stdio.h>
#include <stdint.h>
#include "libgolang.h"

int main()
{
    uint64_t a=10;
    uint64_t b=20;
    uint64_t c=Add(a,b);
    printf("%ld\n",c);
    return 0;
}

在 Windows 中此命令:

go build -buildmode c-archive -o libgolang.a

工作正常并生成libgolang.alibgolang.h文件。 libgolang.h:

/* Created by "go tool cgo" - DO NOT EDIT. */

/* package github.com/ARamazani/go/DLL */

/* Start of preamble from import "C" comments.  */




/* End of preamble from import "C" comments.  */


/* Start of boilerplate cgo prologue.  */

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;

/*
  static assertion to make sure the file is being used on architecture
  at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];

typedef struct { const char *p; GoInt n; } GoString;
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue.  */

#ifdef __cplusplus
extern "C" {
#endif


extern GoUint64 Add(GoUint64 p0, GoUint64 p1);

#ifdef __cplusplus
}
#endif

但是这个命令:

gcc -o test _main.c libgolang.a -lpthread

输出:

libgolang.a(go.o):(.data+0x2150): undefined reference to `NtWaitForSingleObject'
libgolang.a(go.o):(.data+0x21b8): undefined reference to `WSAGetOverlappedResult'
libgolang.a(go.o):(.data+0x21d8): undefined reference to `timeBeginPeriod'
collect2.exe: error: ld returned 1 exit status

go版本go1.7rc3 windows/amd64

我想使用 C 代码中的 libgolang.a 来构建 C 可执行文件:test.exe

有什么解决办法吗?

一些有用的链接:

http://blog.ralch.com/tutorial/golang-sharing-libraries/
Using Go code in an existing C project
Using Go on existing C project

最佳答案

终于找到至少一种方法:
使用此文件main.go:

package main

import "C"
import "fmt"

//export Add
func Add(a, b uint64) uint64 {
    return a + b
}

func main() {
    fmt.Println("Hi")
}

运行此命令:

go build -buildmode c-archive -o libgolang.a

生成libgolang.alibgolang.h 然后使用_main.c:

#include <stdio.h>
#include <stdint.h>
#include "libgolang.h"

int main()
{
    uint64_t a=10;
    uint64_t b=20;
    uint64_t c=Add(a,b);
    printf("%ld\n",c);
    return 0;
}

这就是答案(这对我有用):

gcc -o test _main.c libgolang.a -lWinMM -lntdll -lWS2_32

创建了 test.exe。

运行:

test.exe

输出:

30

关于go - 如何将 Golang 包链接到 Windows x86-64 上的现有 C 项目(使用 Go from C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37987643/

相关文章:

string - 如何使用带界面的 Sscan

go - 获取可为空字符串的长度

arrays - 从另一个函数附加到数组?

go - 下载并准备要在另一台隔离机器上使用的 go 包的依赖项

go - 如何通过client-go在Pod的容器中执行多个命令?

go - vim-go 完成更新后停止工作

go - 使用指定的以太网 IP 发出 http 请求

go - 使用 net/url 值获取错误{}

go - byte slice 操作

Golang 将 float64 转换为 int 错误