cgo中编译选项 - Sat, Oct 24, 2020
cgo中编译选项
package hcnet
/*
#cgo CFLAGS: -I${SRCDIR}
#cgo linux LDFLAGS: -L${SRCDIR}/../../3rdParty/Linux/hc_net_sdk/lib -lhcnetsdk -lhpr -lHCCore -Wl,-rpath=/usr/local/lib
#cgo linux LDFLAGS: -L${SRCDIR}/../../3rdParty/Linux/hc_net_sdk/lib/HCNetSDKCom -lHCGeneralCfgMgr -lHCIndustry -Wl,-rpath=/usr/local/lib/HCNetSDKCom
#cgo windows LDFLAGS: -L${SRCDIR}/../../3rdParty/Windows/hc_net_sdk/lib -lHCCore -lHCNetSDK -lhpr -Wl,-rpath=./
#cgo windows LDFLAGS: -L${SRCDIR}/../../3rdParty/Windows/hc_net_sdk/lib/HCNetSDKCom -lHCGeneralCfgMgr -lHCIndustry -Wl,-rpath=./HCNetSDKCom
#include "HCNetSDK.h"
//#include <stdio.h>
#include <stdlib.h>
//#include <unistd.h>
extern void fRemoteConfigCallbackCgo(DWORD dwType, void* lpBuffer, DWORD dwBufLen, void* pUserData);
extern BOOL MSGCallBackV31Cgo(LONG lCommand, NET_DVR_ALARMER *pAlarmer, char *pAlarmInfo, DWORD dwBufLen, void* pUser);
*/
import "C"
import (
"encoding/hex"
"fmt"
"unsafe"
)
CFLAGS
是引入头文件
${SRCDIR}
是指当前go文件所在的文件夹
LDFLAGS
引入链接库
-L
链接库路径
-l
链接库名
linux
和windows
是跨平台选项
rpath
指编译出的可执行文件依赖的链接库路径
Makefile的写法
build: linux
linux:
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC=gcc go build -o ./bin/ .
windows:
CGO_ENABLED=1 GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc go build -o ./bin/ .
要在linux上编译windows程序需要mingw,在windows中使用cgo也需要mingw