dongli1920 2015-01-23 03:12
浏览 53

在Go程序中嵌入C ++可执行函数

My goal is to include a function I have in a compiled executable into a go program.

My directory structure is set up as follows:

ProjectDir/
    include/
        <header files>
    main.go
    libMyLibrary.dylib
    otherMain.cpp
    otherMain.hpp

My go file is as follows

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());
}

However, when I try to run go build main.go I get the following error from the linker:

# 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)

I've been puzzling over this and can't see as to why the linker can't find the library. Any ideas?

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();
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了