nim 生成动态库(dll)
2015-10-20 15:38
513 查看
nim 编写dll
nim 的dll,手册中说先编译生成 nimrtl.dll 然后其他的dll依赖此dll产生一个新的dll,我们先抛开这个支持GC的选项,生成一个简单的dll
命令行:nim c --cpu:i386 --app:lib mydll
--cpu 指定cpu架构,实验可以是i386和ia64,但是不是intel 架构,是amd64
--app:lib 生成动态库
需要配置自己的config文件
在mydll.nim 的所在目录下创建一个mydll.nim.cfg ,编译器就会使用这个cfg文件编译;可以从nim安装目录的config目录下拿过来nim.cfg改改,改成如下
其中在windows下
@if windows:
cc = vcc
#gcc.path = r"$nimrod\dist\mingw\bin"
tlsEmulation:on
@end
指定编译器是vc,需要将vc的目录(我使用vs2008)放到环境变量中,并且生成 "INCLUDE" 和 "LIB" (环境变量名没有双引号),把自己的INCLUDE 目录(VS的目录和SDK目录的头文件)LIB目录(VS的目录和SDK目录的库文件)写入到INCLUDE和LIB环境变量中
编译后生成的符号为 _test@8 这个是C编译器生成的导出符号,我们想要一个test,则类似vs放一个.def ,vcc.options.linker = "/DEBUG /Zi /Fd\"$projectName.pdb\" \"mydll.def\" "
这样cl.exe 就把这个选项 mydll.def 生成 /DEF:mydll.def 传递给link.exe 去链接
以下是代码:mydll.nim
测试代码如下:test_mydll.nim
通过上面的例子,我们可以使用nim 很容易生成dll。
nim 支持的CPU
支持的操作系统:
nim 的dll,手册中说先编译生成 nimrtl.dll 然后其他的dll依赖此dll产生一个新的dll,我们先抛开这个支持GC的选项,生成一个简单的dll
命令行:nim c --cpu:i386 --app:lib mydll
--cpu 指定cpu架构,实验可以是i386和ia64,但是不是intel 架构,是amd64
--app:lib 生成动态库
需要配置自己的config文件
在mydll.nim 的所在目录下创建一个mydll.nim.cfg ,编译器就会使用这个cfg文件编译;可以从nim安装目录的config目录下拿过来nim.cfg改改,改成如下
# Configuration file for the Nim Compiler. # (c) 2015 Andreas Rumpf # Feel free to edit the default values as you need. # You may set environment variables with # @putenv "key" "val" # Environment variables can be accessed like so: # gcc.path %= "$CC_PATH" cc = gcc # additional options always passed to the compiler: --parallel_build: "0" # 0 to auto-detect number of processors hint[LineTooLong]=off #hint[XDeclaredButNotUsed]=off # example of how to setup a cross-compiler: arm.linux.gcc.exe = "arm-linux-gcc" arm.linux.gcc.linkerexe = "arm-linux-gcc" mips.linux.gcc.exe = "mips-openwrt-linux-gcc" mips.linux.gcc.linkerexe = "mips-openwrt-linux-gcc" @if not nimfix: cs:partial @end path="$lib/core" path="$lib/pure" path="$lib/pure/collections" path="$lib/pure/concurrency" path="$lib/impure" path="$lib/wrappers" # path="$lib/wrappers/cairo" # path="$lib/wrappers/gtk" # path="$lib/wrappers/lua" # path="$lib/wrappers/opengl" path="$lib/wrappers/pcre" path="$lib/wrappers/readline" path="$lib/wrappers/sdl" # path="$lib/wrappers/x11" path="$lib/wrappers/zip" path="$lib/wrappers/libffi" path="$lib/windows" path="$lib/posix" path="$lib/js" path="$lib/pure/unidecode" @if nimbabel: babelpath="$home/.babel/pkgs/" nimblepath="$home/.nimble/pkgs/" @end @if release or quick: obj_checks:off field_checks:off range_checks:off bound_checks:off overflow_checks:off assertions:off stacktrace:off linetrace:off debugger:off line_dir:off dead_code_elim:on @end @if release: opt:speed @end @if unix: @if not bsd: # -fopenmp gcc.options.linker = "-ldl" gcc.cpp.options.linker = "-ldl" clang.options.linker = "-ldl" tcc.options.linker = "-ldl" @end @if bsd or haiku: # BSD got posix_spawn only recently, so we deactivate it for osproc: define:useFork # at least NetBSD has problems with thread local storage: tlsEmulation:on @end @end # Configuration for the Intel C/C++ compiler: @if windows: icl.options.speed = "/Ox /arch:SSE2" icl.options.always = "/nologo" @end # Configuration for the GNU C/C++ compiler: @if windows: #gcc.path = r"$nimrod\dist\mingw\bin" @if gcc: tlsEmulation:on @end @end @if windows: cc = vcc #gcc.path = r"$nimrod\dist\mingw\bin" tlsEmulation:on @end @if macosx or freebsd: cc = clang tlsEmulation:on gcc.options.always = "-w" gcc.cpp.options.always = "-w -fpermissive" @else: gcc.options.always = "-w" gcc.cpp.options.always = "-w -fpermissive" @end # Configuration for Objective-C compiler: # # Options for GNUStep. GNUStep configuration varies wildly, so you'll probably # have to add additional compiler and linker flags on a per-project basis. gcc.objc.options.linker = "-lobjc -lgnustep-base" llvm_gcc.objc.options.linker = "-lobjc -lgnustep-base" clang.objc.options.linker = "-lobjc -lgnustep-base" # Options for Mac OS X. Mac OS X uses its own Objective-C stack that is # totally different from GNUStep. @if macosx: gcc.objc.options.linker = "-framework Foundation" llvm_gcc.objc.options.linker = "-framework Foundation" clang.objc.options.linker = "-framework Foundation" @end # Configuration for the VxWorks # This has been tested with VxWorks 6.9 only @if vxworks: # For now we only support compiling RTPs applications (i.e. no DKMs) gcc.options.always = "-mrtp -fno-strict-aliasing -D_C99 -D_HAS_C9X -std=c99 -fasm -Wall -Wno-write-strings" # The linker config must add the VxWorks common library for the selected # processor which is usually found in: # "$WIND_BASE/target/lib/usr/lib/PROCESSOR_FAMILY/PROCESSOR_TYPE/common", # where PROCESSOR_FAMILY and PROCESSOR_TYPE are those supported by the VxWorks # compiler (e.g. ppc/PPC32 or mips/MIPSI64, etc) # For now we only support the PowerPC CPU gcc.options.linker %= "-L $WIND_BASE/target/lib/usr/lib/ppc/PPC32/common -mrtp -fno-strict-aliasing -D_C99 -D_HAS_C9X -std=c99 -fasm -Wall -Wno-write-strings" @end gcc.options.speed = "-O3 -fno-strict-aliasing" gcc.options.size = "-Os" gcc.options.debug = "-g3 -O0" gcc.cpp.options.speed = "-O3 -fno-strict-aliasing" gcc.cpp.options.size = "-Os" gcc.cpp.options.debug = "-g3 -O0" #passl = "-pg" # Configuration for the LLVM GCC compiler: llvm_gcc.options.debug = "-g" llvm_gcc.options.always = "-w" llvm_gcc.options.speed = "-O2" llvm_gcc.options.size = "-Os" # Configuration for the LLVM CLang compiler: clang.options.debug = "-g" clang.options.always = "-w" clang.options.speed = "-O3" clang.options.size = "-Os" # Configuration for the Visual C/C++ compiler: vcc.options.linker = "/DEBUG /Zi /Fd\"$projectName.pdb\" \"mydll.def\" " vcc.options.debug = "/Zi /Fd\"$projectName.pdb\"" vcc.options.always = "/nologo " vcc.options.speed = "/O2 /arch:SSE2" vcc.options.size = "/O1" # Configuration for the Digital Mars C/C++ compiler: @if windows: dmc.path = r"$nimrod\dist\dm\bin" @end # Configuration for the Tiny C Compiler: tcc.options.always = "-w"
其中在windows下
@if windows:
cc = vcc
#gcc.path = r"$nimrod\dist\mingw\bin"
tlsEmulation:on
@end
指定编译器是vc,需要将vc的目录(我使用vs2008)放到环境变量中,并且生成 "INCLUDE" 和 "LIB" (环境变量名没有双引号),把自己的INCLUDE 目录(VS的目录和SDK目录的头文件)LIB目录(VS的目录和SDK目录的库文件)写入到INCLUDE和LIB环境变量中
编译后生成的符号为 _test@8 这个是C编译器生成的导出符号,我们想要一个test,则类似vs放一个.def ,vcc.options.linker = "/DEBUG /Zi /Fd\"$projectName.pdb\" \"mydll.def\" "
这样cl.exe 就把这个选项 mydll.def 生成 /DEF:mydll.def 传递给link.exe 去链接
以下是代码:mydll.nim
proc test(name:cstring,age:int32):bool {.stdcall,exportc,dynlib.} = if age>18 : echo "name=",name," ","age=",age,">18" true else: echo "name=",name," ","age=",age,"<18" false
测试代码如下:test_mydll.nim
proc test(name:cstring,age:int32):bool {.stdcall,importc:"test",dynlib:"mydll.dll".} echo test("bkdrong",17) echo test("bill",40)
通过上面的例子,我们可以使用nim 很容易生成dll。
nim 支持的CPU
(name: "i386", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32), (name: "m68k", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32), (name: "alpha", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64), (name: "powerpc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32), (name: "powerpc64", intSize: 64, endian: bigEndian, floatSize: 64,bit: 64), (name: "sparc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32), (name: "vm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32), (name: "ia64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64), (name: "amd64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64), (name: "mips", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32), (name: "arm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32), (name: "js", intSize: 32, endian: bigEndian,floatSize: 64,bit: 32), (name: "nimrodvm", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32), (name: "avr", intSize: 16, endian: littleEndian, floatSize: 32, bit: 16)]
支持的操作系统:
OS*: array[succ(low(TSystemOS))..high(TSystemOS), TInfoOS] = [ (name: "DOS", parDir: "..", dllFrmt: "$1.dll", altDirSep: "/", objExt: ".obj", newLine: "\x0D\x0A", pathSep: ";", dirSep: "\\", scriptExt: ".bat", curDir: ".", exeExt: ".exe", extSep: ".", props: {ospCaseInsensitive}), (name: "Windows", parDir: "..", dllFrmt: "$1.dll", altDirSep: "/", objExt: ".obj", newLine: "\x0D\x0A", pathSep: ";", dirSep: "\\", scriptExt: ".bat", curDir: ".", exeExt: ".exe", extSep: ".", props: {ospCaseInsensitive}), (name: "OS2", parDir: "..", dllFrmt: "$1.dll", altDirSep: "/", objExt: ".obj", newLine: "\x0D\x0A", pathSep: ";", dirSep: "\\", scriptExt: ".bat", curDir: ".", exeExt: ".exe", extSep: ".", props: {ospCaseInsensitive}), (name: "Linux", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {ospNeedsPIC, ospPosix}), (name: "MorphOS", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {ospNeedsPIC, ospPosix}), (name: "SkyOS", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {ospNeedsPIC, ospPosix}), (name: "Solaris", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {ospNeedsPIC, ospPosix}), (name: "Irix", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {ospNeedsPIC, ospPosix}), (name: "NetBSD", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {ospNeedsPIC, ospPosix}), (name: "FreeBSD", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {ospNeedsPIC, ospPosix}), (name: "OpenBSD", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {ospNeedsPIC, ospPosix}), (name: "AIX", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {ospNeedsPIC, ospPosix}), (name: "PalmOS", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {ospNeedsPIC}), (name: "QNX", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {ospNeedsPIC, ospPosix}), (name: "Amiga", parDir: "..", dllFrmt: "$1.library", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {ospNeedsPIC}), (name: "Atari", parDir: "..", dllFrmt: "$1.dll", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: "", curDir: ".", exeExt: ".tpp", extSep: ".", props: {ospNeedsPIC}), (name: "Netware", parDir: "..", dllFrmt: "$1.nlm", altDirSep: "/", objExt: "", newLine: "\x0D\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: ".nlm", extSep: ".", props: {ospCaseInsensitive}), (name: "MacOS", parDir: "::", dllFrmt: "$1Lib", altDirSep: ":", objExt: ".o", newLine: "\x0D", pathSep: ",", dirSep: ":", scriptExt: "", curDir: ":", exeExt: "", extSep: ".", props: {ospCaseInsensitive}), (name: "MacOSX", parDir: "..", dllFrmt: "lib$1.dylib", altDirSep: ":", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {ospNeedsPIC, ospPosix, ospLacksThreadVars}), (name: "Haiku", parDir: "..", dllFrmt: "lib$1.so", altDirSep: ":", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {ospNeedsPIC, ospPosix, ospLacksThreadVars}), (name: "VxWorks", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ";", dirSep: "\\", scriptExt: ".sh", curDir: ".", exeExt: ".vxe", extSep: ".", props: {ospNeedsPIC, ospPosix, ospLacksThreadVars}), (name: "JS", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {}), (name: "NimrodVM", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {}), (name: "Standalone", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {})]
相关文章推荐
- coderforce 573A Bear and Poker
- java根据汉字获取拼音的方法
- iOS7 NavigationController 手势问题 interactivePopGestureRecognizer
- Qt自适应大小显示图片,添加菜单
- SSO之CAS单点登录实例演示
- Cocos2d-lua-3.8.1在Cocos code ide中创建新项目的问题
- SQL命令行修改数据库
- 乱七八杂
- 百度地图Api进阶教程-点击生成和拖动标注4.html
- 容量预估、压测过程中总结
- 覆铜规则
- R - plot(density)
- IE浏览器如何调试
- Swift百万线程攻破单例(Singleton)模式
- 单文档标题的修改
- 系统侧滑返回
- Python – locals和globals
- python视频教程大全
- 【网络安全】移动安全软件合集
- Ruby的基础语法入门学习教程