duangu6588 2018-09-23 12:00
浏览 75

用CGO构建git子模块

I need to include a C++ code from git submodule to my GO program and have following code:

$ tree
├── git-imaginary-submodule
│   ├── test.cc
│   └── test.h
└── main.go

1 directory, 3 files

$ cat main.go
package main

//#cgo CPPFLAGS: -g -I${SRCDIR}/git-imaginary-submodule
//#cgo CFLAGS: -g -I${SRCDIR}/git-imaginary-submodule
//#include "test.h"
import "C"

import (
    "log"
)

func main() {
    log.Println(C.test())
}

When I run go build I have following:

$ go build
# github.com/shagabutdinov/stackoverflow-go-include-question/nope
/usr/bin/ld: $WORK/b001/_x002.o: in function `_cgo_bc718ee76ee7_Cfunc_test':
/tmp/go-build/cgo-gcc-prolog:43: undefined reference to `test'
collect2: error: ld returned 1 exit status

But when the C++ code is locally located build is fine:

$ tree
.
├── main.go
├── test.cc
└── test.h

0 directories, 3 files
$ go build .
# ok

$ cat main.go 
package main

//#include "test.h"
import "C"

import (
  "log"
)

func main() {
  log.Println(C.test())
}

Here are source code for test.h and test.cc:

$ cat test.h 
#ifdef __cplusplus
extern "C" {
#endif
  int test();
#ifdef __cplusplus
}
#endif

$ cat test.cc
#include <vector>
#include "test.h"

int test() {
  std::vector<int> vector = {1, 2};
  return vector[0] + vector[1];
}

How can I use C++ code located in another directory?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测