【发布时间】:2013-05-06 07:31:54
【问题描述】:
我正在尝试用 C 语言构建一些本机库,以加快 Java 项目的蒙特卡罗模拟。我在 Netbeans 7.0.1 上使用 MINGW 进行此操作
我正在使用 Geof Kuenning (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html) 的 mtwist 源代码来提供 Mersenne Twister 伪随机数。
我要模拟三个不同的模型,每个模型都可以自行编译(输出到文件而不是使用 JNI)。但是,当我将 JNI 头文件下的三个模型组合起来时,我在尝试构建时开始得到一长串错误:
清洁成功(总时间:7s) “/usr/bin/make”-f nbproject/Makefile-Debug.mk QMAKE=SUBPROJECTS=.build-conf make[1]: 进入目录
/c/Users/Tom/Documents/LongMemoryModels/longMemModels' "/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW_1- Windows/liblongMemModels.dll make[2]: Entering directory/c/Users/Tom/Documents/LongMemoryModels/longMemModels' mkdir -p 构建/调试/MinGW_1-Windows rm -f build/Debug/MinGW_1-Windows/longmem.o.d gcc.exe -std=c99 -c -g -I../../../../../Program\ Files/Java/jdk1.7.0/include/ -I../../.. /../../Program\ Files/Java/jdk1.7.0/include/win32 -MMD -MP -MF build/Debug/MinGW_1-Windows/longmem.o.d -o build/Debug/MinGW_1-Windows/longmem.o longmem.c mkdir -p build/Debug/MinGW_1-Windows/_ext/2069311947 rm -f build/Debug/MinGW_1-Windows/_ext/2069311947/mtwist.o.d gcc.exe -std=c99 -c -g -I../../../../../Program\ Files/Java/jdk1.7.0/include/ -I../../.. /../../Program\ Files/Java/jdk1.7.0/include/win32 -MMD -MP -MF build/Debug/MinGW_1-Windows/_ext/2069311947/mtwist.o.d -o build/Debug/MinGW_1-Windows /_ext/2069311947/mtwist.o ../../../../../cinclude/mtwist/mtwist.c mkdir -p build/Debug/MinGW_1-Windows/_ext/2069311947 rm -f build/Debug/MinGW_1-Windows/_ext/2069311947/randistrs.o.d gcc.exe -std=c99 -c -g -I../../../../../Program\ Files/Java/jdk1.7.0/include/ -I../../.. /../../Program\ Files/Java/jdk1.7.0/include/win32 -MMD -MP -MF build/Debug/MinGW_1-Windows/_ext/2069311947/randistrs.o.d -o build/Debug/MinGW_1-Windows /_ext/2069311947/randistrs.o ../../../../../cinclude/mtwist/randistrs.c mkdir -p dist/Debug/MinGW_1-Windows gcc.exe -std=c99 -shared -o dist/Debug/MinGW_1-Windows/liblongMemModels.dll build/Debug/MinGW_1-Windows/longmem.o build/Debug/MinGW_1-Windows/_ext/2069311947/mtwist.o > build /Debug/MinGW_1-Windows/_ext/2069311947/randistrs.o
build/Debug/MinGW_1-Windows/_ext/2069311947/mtwist.o:在函数mts_lrand': C:\Users\Tom\Documents\LongMemoryModels\longMemModels/../../../../../cinclude/mtwist/mtwist.h:402: multiple definition ofmts_lrand' build/Debug/MinGW_1-Windows/longmem.o:C:/cinclude/mtwist/mtwist.h:402: 这里首先定义 build/Debug/MinGW_1-Windows/_ext/2069311947/mtwist.o:在函数mts_llrand': C:\Users\Tom\Documents\LongMemoryModels\longMemModels/../../../../../cinclude/mtwist/mtwist.h:431: multiple definition ofmts_llrand' build/Debug/MinGW_1-Windows/longmem.o:C:/cinclude/mtwist/mtwist.h:431: 首先定义在这里 [...] build/Debug/MinGW_1-Windows/_ext/2069311947/randistrs.o:在函数mts_lrand': C:\Users\Tom\Documents\LongMemoryModels\longMemModels/../../../../../cinclude/mtwist/mtwist.h:402: multiple definition ofmts_lrand' build/Debug/MinGW_1-Windows/longmem.o:C:/cinclude/mtwist/mtwist.h:402: 这里首先定义 build/Debug/MinGW_1-Windows/_ext/2069311947/randistrs.o:在函数mts_llrand': C:\Users\Tom\Documents\LongMemoryModels\longMemModels/../../../../../cinclude/mtwist/mtwist.h:431: multiple definition ofmts_llrand' build/Debug/MinGW_1-Windows/longmem.o:C:/cinclude/mtwist/mtwist.h:431: 首先定义在这里 [...] make[2]: 离开目录/c/Users/Tom/Documents/LongMemoryModels/longMemModels' make[1]: Leaving directory/c/Users/Tom/Documents/LongMemoryModels/longMemModels'collect2.exe:错误:ld 返回 1 个退出状态 make[2]: * [dist/Debug/MinGW_1-Windows/liblongMemModels.dll] 错误 1 make[1]: [.build-conf] 错误 2 make: ** [.build-impl] 错误 2
BUILD FAILED(退出值2,总时间:22s)
(这里的省略号 [...] 表示我省略的类似错误)
如您所见,多个包含似乎与 mtwist.h 标头有关,而不是与 JNI 标头有关。此外,即使 mtwist 已经成功构建为静态或动态库,我在尝试构建 JNI 项目时也会遇到同样的错误......
我不希望这个问题太像一篇文章,我希望这个错误会向比我更精明的人揭示一个明显的错误。但是,如果这还不足以指出解决方案,我也会发布源代码。
如果可以的话,请帮我解决这个构建错误。 汤姆
编辑:使包含结构比 cmets 更清晰... 在目前的安排中:
- [JNI 模型头文件].h 包含 jni.h
- mtwist.h 包括 stdio.h、stdint.h
- randistrs.h,包括 mtwist.h
- [单个模型函数].c 包括 stdio.h stdlib.h math.h mtwist/mtwist.h mtwist/randistrs.h jni.h(我想没有必要)
- mtwist.c 包括 inttypes.h stdio.h stdlib.h sys/timeb.h mtwist.h
- randistrs.c 包括 mtwist.h randistrs.h math.h stdlib.h
所有 .h 文件都有使用 #ifndef 的包含保护
【问题讨论】:
-
当两个(或更多)变量/函数在一个范围内具有完全相同的名称时,会出现多重定义错误。你确定你的三个模型是一起编译的吗?
-
感谢 MisterJ,但这三个模型非常简单——每个模型只包含一个函数,每个函数都有一个不同的名称。更重要的是,无论所有三个函数都列在同一个 .c 文件中还是位于具有单独包含的单独文件中,我都会得到相同的构建错误。
-
听起来像是多重包含。从您的主文件开始,究竟哪些文件包含在哪里?
-
所以,在目前的安排中:[JNI 模型头].h 包括
; mtwist.h 包括 的包含保护; randistrs.h 包括“mtwist.h”; [个别模型函数].c 包括 (我想没有必要); mtwist.c 包括 ; randistrs.c 包括 "mtwist.h" "randistrs.h" ;所有 .h 文件都有使用 #ifndef
标签: c java-native-interface multiple-definition-error