为什么使用 Cgo 时我的 .go 文件中无法识别 c 函数?我遵循了所有过程并尝试了godoc上的示例,它可以工作,但是这个不起作用,是什么原因?
文件夹结构
libsha.a
sha.cpp
扫码
沙.h
main.go
代码
沙.h
#ifndef _SHA_H_
#define _SHA_H_
#include <stdlib.h>
#include "TYPE.h"
typedef struct {
U32 bits[2];
U32 input[16];
U32 state[5];
} SHA_CTX;
void SHA_Init(SHA_CTX *ctx);
void SHA_Update(SHA_CTX *ctx, U8 *in, int inbytes);
void SHA_Final(SHA_CTX *ctx, U8 *out);
void KS_SHA(U8 *out, U8 *in, int inbytes);
#endif
sha.cpp
#include "sha.h"
void SHA_Init(SHA_CTX *ctx)
{
ctx->state[0] = INIT_H0;
ctx->state[1] = INIT_H1;
ctx->state[2] = INIT_H2;
ctx->state[3] = INIT_H3;
ctx->state[4] = INIT_H4;
ctx->bits[0] = ctx->bits[1] = 0;
}
main.go
package main
// #cgo LDFLAGS: -L . -lsha
// #include "sha.h"
import "C"
import "unsafe"
type CPoint struct {
Point C.struct_SHA_CTX
}
func main() {
point := CPoint{Point: C.struct_SHA_CTX{}}
C.SHA_Init(point)
defer C.free(unsafe.Pointer(point))
}
错误
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: $WORK\b001\_x002.o: in function `_cgo_6280fd3fea2a_Cfunc_SHA_Init':
/tmp/go-build/cgo-gcc-prolog:49: undefined reference to `SHA_Init'
collect2.exe: error: ld returned 1 exit status
为什么无法识别 SHA_Init 函数?
梦里花落0921
慕尼黑的夜晚无繁华
随时随地看视频慕课网APP
相关分类