go中调用c库的问题

祝云飞 2017-05-09 02:55:19
.h

#ifndef ALGORITHM_DLL_H
#define ALGORITHM_DLL_H

#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) int add(int a, int b);
__declspec(dllexport) int sub(int a, int b);

#ifdef __cplusplus
}
#endif


.cpp

#include "Algorithm.h"
int add(int a, int b)
{
return a + b;
}

int sub(int a, int b)
{
return a - b;
}


将以上文件用vs编译成Algorithm.dll后,拷贝到go所建的包内
结构如下:
|-project
| |-lib
| | |-Algorithm.dll
| |-include
| | |-Algorithm.h
| | main.go


// main.go
package main

/*
#cgo CFLAGS: -Iinclude
#cgo LDFLAGS: -Llib -lAlgorithm
#include "Algorithm.h"
*/
import "C"

import (
"fmt"
)

func main() {
fmt.Println("Hello World!")
ret := int32(C.add(2, 3))
fmt.Println(ret)
}


结果提示程序崩溃,无法达到输出执行加法的结果
...全文
80 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
祝云飞 2017-05-09
  • 打赏
  • 举报
回复
已解决,原因就是没有把库文件和go生成的程序放在同一文件夹下

2,190

社区成员

发帖
与我相关
我的任务
社区描述
go语言学习与交流版
社区管理员
  • go语言社区
  • Freeman Z
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧