我的目标是将已编译的可执行文件中的函数包含到go程序中。
我的目录结构设置如下:
ProjectDir/
include/
<header files>
main.go
libMyLibrary.dylib
otherMain.cpp
otherMain.hpp
我的go文件如下
package main
/*
#cgo CFLAGS: -I include
#cgo LDFLAGS: -framework Cocoa -L ./ -lMyLibrary
#include "otherMain.hpp"
gtype_real64 myFunction();
*/
import "C"
import "fmt"
func main() {
fmt.Println(C.myFunction());
}
但是,当我尝试运行go build main.go
时,我从链接器中收到以下错误:
# command-line-arguments
Undefined symbols for architecture x86_64:
"_myFunction", referenced from:
__cgo_159d40466606_Cfunc_myFunction in main.cgo2.o
(maybe you meant: __cgo_159d40466606_Cfunc_myFunction)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我一直在为此感到困惑,无法看到链接器无法找到库的原因。有什么想法吗?
otherMain.hpp:
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <Carbon/Carbon.h>
#define MAX_NUM_MEASUREMENTS 100
#include "<a header file in include>.h"
int main(int argc, char* argv[]);
gtype_real64 myFunction();