[error] Can't load IA 32-bit .dll on a AMD 64-bit plat

来源:互联网 发布:找不到网络共享文件夹 编辑:程序博客网 时间:2024/05/07 22:15

刚刚在使用JNI(Java Native Interface)的时候,准备把C程序在windows命令行或Visual Studio Command Prompt(2013)命令行下使用VS的cl命令编译链接生成dll动态链接库文件。所以按照传统方法在命令行vs安装目录/common7/Tools/下执行vsvars32.bat,然后执行cl指令。问题来了,这个vsvars32.bat相当于配置了32位系统环境为编译平台,所以在64位机器上加载调用生成的32位DLL文件就报错了。


这里http://blog.csdn.net/lijie45655/article/details/6730885有一篇关于cl命令的详解,本想通过/MACHINE选项来指定64位平台的,但不可行。。


后来找到这里https://msdn.microsoft.com/en-us/library/x4d2c09s.aspx,点击打开链接 (下面做个内容备份)


Visual Studio includes 32-bit, x86-hosted, native and cross compilers for x86, x64, and ARM targets. When Visual Studio is installed on a 64-bit Windows operating system, 32-bit, x86-hosted native and cross compilers, and also 64-bit, x64-hosted native and cross compilers, are installed for each target (x86, x64, and ARM). The 32-bit and 64-bit compilers for each target generate identical code, but the 64-bit compilers support more memory for precompiled header symbols and the Whole Program Optimization (/GL, /LTCG) options. If you run into memory limits when you use a 32-bit compiler, try the 64-bit compiler.

When Visual Studio is installed on a 64-bit Windows operating system, additional Command Prompt shortcuts for the 64-bit x64-native and x86 cross compilers are available. To access these command prompts on Windows 8, on the Start screen, open All apps. Under the installed version of Visual Studio, open Visual Studio Tools, and then choose one of the native-tool or cross-tool command prompts. On earlier versions of Windows, choose Start, expand All ProgramsVisual StudioVisual Studio Tools, and then choose a command prompt.

Vcvarsall.bat

Any of the compilers can be used on the command line by running thevcvarsall.bat command file to configure the path and environment variables that enable the compiler toolset. Because there are no Command Prompt shortcuts to enable a 64-bit toolset to target x86 or ARM platforms, use vcvarsall.bat in a Command Prompt window to use the 64-bit toolset instead.For more information, see Setting the Path and Environment Variables for Command-Line Builds.

The following steps show how to configure a Command Prompt to use the 64-bit native toolset to target x86, x64, and ARM platforms.

To run vcvarsall.bat to use a 64-bit toolset

  1. At the command prompt, change to the Visual C++ installation directory. (The location depends on the system and the Visual Studio installation, but a typical location is C:\Program Files (x86)\Microsoft Visual Studio version\VC\.) For example, enter:

    cd "\Program Files (x86)\Microsoft Visual Studio 12.0\VC"

  2. To configure this Command Prompt window for 64-bit command-line builds that targetx64 platforms, at the command prompt, enter:

    vcvarsall amd64    //运行这条指令就可以配置目标系统平台为64位机了

  3. To configure this Command Prompt window for 64-bit command-line builds that targetx86 platforms, at the command prompt, enter:

    vcvarsall amd64_x86

  4. To configure this Command Prompt window for 64-bit command-line builds that targetARM platforms, at the command prompt, enter:

    vcvarsall amd64_arm

我的运行顺序如下:

D:\Program Files (x86)\Microsoft Visual Studio 12.0\VC>vcvarsall amd64


D:\Program Files (x86)\Microsoft Visual Studio 12.0\VC>cd D:\WorkSpace\eclipseWorkspace\JNI-example\src


D:\WorkSpace\eclipseWorkspace\JNI-example\src>cl /LD "factorial.c"  /I "C:\Program Files\Java\jdk1.8.0_65\include" /I "C:\Program Files\Java\jdk1.8.0_65\include\win32"
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.


factorial.c
Microsoft (R) Incremental Linker Version 12.00.21005.1
Copyright (C) Microsoft Corporation.  All rights reserved.


/out:factorial.dll
/dll
/implib:factorial.lib
factorial.obj
   Creating library factorial.lib and object factorial.exp


D:\WorkSpace\eclipseWorkspace\JNI-example\src>javac JniApp.java


D:\WorkSpace\eclipseWorkspace\JNI-example\src>java JniApp
Factorial of 5 is: 120

0 0