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

时间:2016-06-23 09:32:13

标签: go

如何使用来自C代码的Golang Generated 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 version 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

1 个答案:

答案 0 :(得分:2)

终于找到了至少一种方式:
使用此文件sum.Average().ToString()

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.a 然后使用libgolang.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;
}

test.exe创建。

运行:

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

输出:

test.exe