我在使用c++动态链接库a时,有一个原本的main函数,可以调用这个动态链接库
//初始的main.cpp
#define __debug__
#define __timer__
#include "linear_gkr/zk_verifier.h"
#include "linear_gkr/zk_prover.h"
#include "linear_gkr/prime_field.h"
#include <iostream>
#include <cassert>
zk_verifier v;
zk_prover p;
int main(int argc, char** argv)
{
//std::cout << "hello world" << std::endl;
prime_field::init();
p.total_time = 0;
v.get_prover(&p);
//std::cout << "come in" << std::endl;
assert(argc == 4);
v.read_circuit(argv[1], argv[2]);
//std::cout << "after readfile" << std::endl;
p.get_circuit(v.C);
bool result = v.verify(argv[3]);
printf("%s\n", result ? "Pass" : "Fail");
return 0;
}
为了用在golang中使用,我修改了这个main函数为test.cpp与test.h
//test.h
#pragma once
#ifdef _cplusplus
extern "C"{
#endif
void test(char** argv);
#ifdef _cplusplus
}
#endif
//test.cpp
#include "linear_gkr/zk_verifier.h"
#include "linear_gkr/zk_prover.h"
#include "linear_gkr/prime_field.h"
#include "test/virgo.h"
#include <iostream>
#include <cassert>
void test(char** argv)
{
//std::cout << "hello world" << std::endl;
zk_prover p;
zk_verifier v;
prime_field::init();
p.total_time = 0;
v.get_prover(&p);
//std::cout << "come in" << std::endl;
v.read_circuit(argv[0], argv[1]);
//std::cout << "after readfile" << std::endl;
p.get_circuit(v.C);
bool result = v.verify(argv[2]);
printf("%s\n", result ? "Pass" : "Fail");
}
然后我将原本的动态链接库和这个test.cpp编译为一个新的动态链接库libtest.so,然后试图在golang中直接引用
//main.go
package main
/*
#cgo CFLAGS: -I ./include
#cgo LDFLAGS: -L ./src -ltest -lstdc++
#include "test/test.h"
*/
import "C"
import "fmt"
import "unsafe"
func main(){
box := []string{"lanczos2_112_N=16_circuit.txt","lanczos2_112_N=16_meta.txt","LOG/lanczos2_112_N=16.txt"}
var buf []*C.char
for i,_ := range parms {
buf = append(buf, (*C.char)(unsafe.Pointer(C.CString(box[i]))))
}
parms := (**C.char)(unsafe.Pointer(&buf[0]))
C.test(parms)
}
_cgo_1e2644dc5b75_Cfunc_test': /tmp/go-build/cgo-gcc-prolog:49: undefined reference to
补充:运行环境ubuntu20.04,golanh1.15.8
问题发生在go build阶段