尝试编译 Go 共享对象以通过 JNI

如何解决尝试编译 Go 共享对象以通过 JNI

我正在尝试通过 JNI 调用从 Java 调用 Go 函数。 Java编译没问题。 当我尝试构建 Go 共享对象 (.so) 时,它给了我关于可从 Java 调用的 C 函数包装器的“多重定义”错误。

这是Java代码:

package yada.yada.locksmith;

import java.io.*;

public class Locksmith {

   private native void setup(String ClientID,String ClientSecret,String RedirectURL,String AuthURL,String TokenURL,String UserInfURL);
   private native String auth(String user,String pw);

   static {
      System.loadLibrary("Locksmith");
   }

   public static void Locksmith(String[] args) {
      Locksmith locksmith = new Locksmith();

      locksmith.setup(
         "yadayadayadayadayadayadayadayadayadayada","yadayadayadayadayadayadayadayadayadayada","https://yada.yada/Yada/yada","https://yada.yada/Yada/yada2","https://yada.yada/Yada/yada3","https://yada.yada/Yada/yada4"
      );

      // Create the console object
      Console cnsl = System.console();

      if (cnsl == null) {
         System.out.println("No console available");
         return;
      }

      String user = cnsl.readLine("Enter username : ");
      char[] pw = cnsl.readPassword("Enter password : ");
      System.out.println(locksmith.auth(user,new String(pw)));
   }
}

我编译它:

javac Locksmith.java

然后我生成了头文件:

javac -h 。锁匠.java

这是生成的文件:


/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class yada_yada_locksmith_Locksmith */

#ifndef _Included_yada_yada_locksmith_Locksmith
#define _Included_yada_yada_locksmith_Locksmith
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     yada_yada_locksmith_Locksmith
 * Method:    setup
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_yada_yada_locksmith_Locksmith_setup
  (JNIEnv *,jobject,jstring,jstring);

/*
 * Class:     yada_yada_locksmith_Locksmith
 * Method:    auth
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_yada_yada_locksmith_Locksmith_auth
  (JNIEnv *,jstring);

#ifdef __cplusplus
}
#endif
#endif

然后我编写了 Go 动态对象:


package main

import (
//   "io"
//   "fmt"
   "log"
   "sync"
   "net/url"
   "strings"
   "context"
   "net/http"
   "io/ioutil"
   "golang.org/x/oauth2"
   "github.com/chromedp/chromedp"
   "github.com/chromedp/cdproto/network"
)

/*
#cgo CFLAGS: -I/usr/local/bin/jdk-15.0.1/include -I/usr/local/bin/jdk-15.0.1/include/linux

#include <string.h>
#include <jni.h>        // JNI header provided by JDK
#include "yada_yada_locksmith_Locksmith.h"

extern void Setup(char *,char *,char *);
extern char *Auth(char *,char *);

JNIEXPORT void JNICALL Java_yada_yada_locksmith_Locksmith_setup
  (JNIEnv *env,jobject this,jstring ClientID,jstring ClientSecret,jstring RedirectURL,jstring AuthURL,jstring TokenURL,jstring UserInfURL) {

   char *CClientID;
   char *CClientSecret;
   char *CRedirectURL;
   char *CAuthURL;
   char *CTokenURL;
   char *CUserInfURL;

   CClientID = ((char *)(*env)->GetStringUTFChars(env,ClientID,0));
   CClientSecret = ((char *)(*env)->GetStringUTFChars(env,ClientSecret,0));
   CRedirectURL = ((char *)(*env)->GetStringUTFChars(env,RedirectURL,0));
   CAuthURL = ((char *)(*env)->GetStringUTFChars(env,AuthURL,0));
   CTokenURL = ((char *)(*env)->GetStringUTFChars(env,TokenURL,0));
   CUserInfURL = ((char *)(*env)->GetStringUTFChars(env,UserInfURL,0));

   Setup(CClientID,CClientSecret,CRedirectURL,CAuthURL,CTokenURL,CUserInfURL);

   (*env)->ReleaseStringUTFChars(env,CClientID);
   (*env)->ReleaseStringUTFChars(env,CClientSecret);
   (*env)->ReleaseStringUTFChars(env,CRedirectURL);
   (*env)->ReleaseStringUTFChars(env,CAuthURL);
   (*env)->ReleaseStringUTFChars(env,CTokenURL);
   (*env)->ReleaseStringUTFChars(env,CUserInfURL);
}

JNIEXPORT jstring JNICALL Java_yada_yada_locksmith_Locksmith_auth
  (JNIEnv *env,jstring User,jstring Pw) {

   char *CUser;
   char *CPw;

   CUser = ((char *)(*env)->GetStringUTFChars(env,User,0));
   CPw = ((char *)(*env)->GetStringUTFChars(env,Pw,0));


   // Call
   Auth(CUser,CPw);

   (*env)->ReleaseStringUTFChars(env,CUser);
   (*env)->ReleaseStringUTFChars(env,CPw);
}


*/
import "C"

type oauthConfT struct {
   Cfg *oauth2.Config
   UserInfURL string
}

func main() {
}

var cfg oauthConfT

//export Setup
func Setup(ClientID,UserInfURL *C.char) {
   // DO stuff
}

//export Auth
func Auth(user,pw *C.char) *C.char {
   // DO stuff
}


然后我编译 Go 代码:

go build -o liblocksmith.so -buildmode=c-shared locksmith.go

这给出了以下错误消息:

/tmp/go-build051144078/b001/_x002.o: In function "Java_yada_yada_locksmith_Locksmith_setup":
./locksmith.go:29: multiple definition in "Java_yada_yada_locksmith_Locksmith_setup"
/tmp/go-build051144078/b001/_x001.o:/tmp/go-build/locksmith.go:29: defined first here
/tmp/go-build051144078/b001/_x002.o: In function "Java_yada_yada_locksmith_Locksmith_auth":
./locksmith.go:56: multiple definition in "Java_yada_yada_locksmith_Locksmith_auth"
/tmp/go-build051144078/b001/_x001.o:/tmp/go-build/locksmith.go:56: defined first here
collect2: error: ld returned 1 exit status

尝试编译添加 -x 标志会导致:


WORK=/tmp/go-build051144078
mkdir -p $WORK/b056/
cd /home/vuco/repos/go/src/runtime/cgo
CGO_LDFLAGS='"-g" "-O2" "-lpthread"' /home/vuco/repos/go/pkg/tool/linux_amd64/cgo -objdir $WORK/b056/ -importpath runtime/cgo -import_runtime_cgo=false -import_syscall=false -exportheader=$WORK/b056/_cgo_install.h -- -I $WORK/b056/ -g -O2 -Wall -Werror ./cgo.go
cd $WORK
gcc -fno-caret-diagnostics -c -x c - -o /dev/null || true
gcc -Qunused-arguments -c -x c - -o /dev/null || true
gcc -fdebug-prefix-map=a=b -c -x c - -o /dev/null || true
gcc -gno-record-gcc-switches -c -x c - -o /dev/null || true
cd $WORK/b056
TERM='dumb' gcc -I /home/vuco/repos/go/src/runtime/cgo -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b056=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -Wall -Werror -o ./_x001.o -c _cgo_export.c
TERM='dumb' gcc -I /home/vuco/repos/go/src/runtime/cgo -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b056=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -Wall -Werror -o ./_x002.o -c cgo.cgo2.c
cd /home/vuco/repos/go/src/runtime/cgo
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b056=/tmp/go-build -gno-record-gcc-switches -I $WORK/b056/ -g -O2 -Wall -Werror -o $WORK/b056/_x003.o -c gcc_context.c
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b056=/tmp/go-build -gno-record-gcc-switches -I $WORK/b056/ -g -O2 -Wall -Werror -o $WORK/b056/_x004.o -c gcc_fatalf.c
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b056=/tmp/go-build -gno-record-gcc-switches -I $WORK/b056/ -g -O2 -Wall -Werror -o $WORK/b056/_x005.o -c gcc_libinit.c
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b056=/tmp/go-build -gno-record-gcc-switches -I $WORK/b056/ -g -O2 -Wall -Werror -o $WORK/b056/_x006.o -c gcc_linux_amd64.c
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b056=/tmp/go-build -gno-record-gcc-switches -I $WORK/b056/ -g -O2 -Wall -Werror -o $WORK/b056/_x007.o -c gcc_mmap.c
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b056=/tmp/go-build -gno-record-gcc-switches -I $WORK/b056/ -g -O2 -Wall -Werror -o $WORK/b056/_x008.o -c gcc_setenv.c
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b056=/tmp/go-build -gno-record-gcc-switches -I $WORK/b056/ -g -O2 -Wall -Werror -o $WORK/b056/_x009.o -c gcc_sigaction.c
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b056=/tmp/go-build -gno-record-gcc-switches -I $WORK/b056/ -g -O2 -Wall -Werror -o $WORK/b056/_x010.o -c gcc_traceback.c
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b056=/tmp/go-build -gno-record-gcc-switches -I $WORK/b056/ -g -O2 -Wall -Werror -o $WORK/b056/_x011.o -c gcc_util.c
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b056=/tmp/go-build -gno-record-gcc-switches -I $WORK/b056/ -g -O2 -Wall -Werror -o $WORK/b056/_x012.o -c gcc_amd64.S
cd $WORK/b056
TERM='dumb' gcc -I /home/vuco/repos/go/src/runtime/cgo -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b056=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -Wall -Werror -o ./_cgo_main.o -c _cgo_main.c
cd /home/vuco/repos/go/src/runtime/cgo
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b056=/tmp/go-build -gno-record-gcc-switches -o $WORK/b056/_cgo_.o $WORK/b056/_cgo_main.o $WORK/b056/_x001.o $WORK/b056/_x002.o $WORK/b056/_x003.o $WORK/b056/_x004.o $WORK/b056/_x005.o $WORK/b056/_x006.o $WORK/b056/_x007.o $WORK/b056/_x008.o $WORK/b056/_x009.o $WORK/b056/_x010.o $WORK/b056/_x011.o $WORK/b056/_x012.o -g -O2 -lpthread
TERM='dumb' /home/vuco/repos/go/pkg/tool/linux_amd64/cgo -dynpackage cgo -dynimport $WORK/b056/_cgo_.o -dynout $WORK/b056/_cgo_import.go -dynlinker
mkdir -p $WORK/b051/
cd /home/vuco/repos/go/src/net
CGO_LDFLAGS='"-g" "-O2"' /home/vuco/repos/go/pkg/tool/linux_amd64/cgo -objdir $WORK/b051/ -importpath net -exportheader=$WORK/b051/_cgo_install.h -- -I $WORK/b051/ -g -O2 ./cgo_linux.go ./cgo_resnew.go ./cgo_socknew.go ./cgo_unix.go
cd $WORK/b051
TERM='dumb' gcc -I /home/vuco/repos/go/src/net -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b051=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_x001.o -c _cgo_export.c
TERM='dumb' gcc -I /home/vuco/repos/go/src/net -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b051=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_x002.o -c cgo_linux.cgo2.c
TERM='dumb' gcc -I /home/vuco/repos/go/src/net -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b051=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_x003.o -c cgo_resnew.cgo2.c
TERM='dumb' gcc -I /home/vuco/repos/go/src/net -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b051=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_x004.o -c cgo_socknew.cgo2.c
TERM='dumb' gcc -I /home/vuco/repos/go/src/net -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b051=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_x005.o -c cgo_unix.cgo2.c
TERM='dumb' gcc -I /home/vuco/repos/go/src/net -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b051=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -o ./_cgo_main.o -c _cgo_main.c
cd /home/vuco/repos/go/src/net
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b051=/tmp/go-build -gno-record-gcc-switches -o $WORK/b051/_cgo_.o $WORK/b051/_cgo_main.o $WORK/b051/_x001.o $WORK/b051/_x002.o $WORK/b051/_x003.o $WORK/b051/_x004.o $WORK/b051/_x005.o -g -O2
TERM='dumb' /home/vuco/repos/go/pkg/tool/linux_amd64/cgo -dynpackage net -dynimport $WORK/b051/_cgo_.o -dynout $WORK/b051/_cgo_import.go
mkdir -p $WORK/b001/
cd /home/vuco/repos/yada/src/main/java/yada/yada/locksmith
CGO_LDFLAGS='"-g" "-O2"' /home/vuco/repos/go/pkg/tool/linux_amd64/cgo -objdir $WORK/b001/ -importpath command-line-arguments -exportheader=$WORK/b001/_cgo_install.h -- -I $WORK/b001/ -g -O2 -I/usr/local/bin/jdk-15.0.1/include -I/usr/local/bin/jdk-15.0.1/include/linux ./locksmith.go
cd $WORK/b001
TERM='dumb' gcc -I /home/vuco/repos/yada/src/main/java/yada/yada/locksmith -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -I/usr/local/bin/jdk-15.0.1/include -I/usr/local/bin/jdk-15.0.1/include/linux -o ./_x001.o -c _cgo_export.c
TERM='dumb' gcc -I /home/vuco/repos/yada/src/main/java/yada/yada/locksmith -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -I/usr/local/bin/jdk-15.0.1/include -I/usr/local/bin/jdk-15.0.1/include/linux -o ./_x002.o -c locksmith.cgo2.c
TERM='dumb' gcc -I /home/vuco/repos/yada/src/main/java/yada/yada/locksmith -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -I/usr/local/bin/jdk-15.0.1/include -I/usr/local/bin/jdk-15.0.1/include/linux -o ./_cgo_main.o -c _cgo_main.c
cd /home/vuco/repos/yada/src/main/java/yada/yada/locksmith
TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -o $WORK/b001/_cgo_.o $WORK/b001/_cgo_main.o $WORK/b001/_x001.o $WORK/b001/_x002.o -g -O2
# command-line-arguments
/tmp/go-build051144078/b001/_x002.o: In function "Java_yada_yada_locksmith_Locksmith_setup":
./locksmith.go:29: multiple definition in "Java_yada_yada_locksmith_Locksmith_setup"
/tmp/go-build051144078/b001/_x001.o:/tmp/go-build/locksmith.go:29: defined first here
/tmp/go-build051144078/b001/_x002.o: In function "Java_yada_yada_locksmith_Locksmith_auth":
./locksmith.go:56: multiple definition in "Java_yada_yada_locksmith_Locksmith_auth"
/tmp/go-build051144078/b001/_x001.o:/tmp/go-build/locksmith.go:56: defined first here
collect2: error: ld returned 1 exit status

程序版本为:

go 版本 go1.15.6 linux/amd64

javac 15.0.1

gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

GNU ld(Ubuntu 的 GNU Binutils)2.30

解决方法

cgo documentation 中的以下内容是问题所在:

在文件中使用 //export 会限制序言:因为它被复制到两个不同的 C 输出文件中,所以它不能包含任何定义,只能包含声明。如果一个文件同时包含定义和声明,那么这两个输出文件将产生重复的符号并且链接器将失败。为避免这种情况,必须将定义放在其他文件或 C 源文件的序言中。

移动线条

extern void Setup(char *,char *,char *);
extern char *Auth(char *,char *);

文件 locksmith.h 和 C 定义 locksmith.c 将产生以下序言,它成功构建:

/*
#cgo CFLAGS: -I/usr/local/bin/jdk-15.0.1/include -I/usr/local/bin/jdk-15.0.1/include/linux

#include "locksmith.h"
*/
import "C"

locksmith.c 的开头将包含以下内容:

#include <string.h>
#include <jni.h>        // JNI header provided by JDK
#include "locksmith.h"
#include "yada_yada_locksmith_Locksmith.h"

此外,构建命令需要只是

go build -o liblocksmith.so -buildmode=c-shared

末尾没有 locksmith.go

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-