如何编译用C编写的64位dll? [英] How to compile a 64-bit dll written in C?
问题描述
我有一个Java程序,该程序利用一些本机函数调用来加快视频编码。它需要一个DLL,我将用C编写该DLL(我现在只有一个测试)。
I have a Java program which makes use of some native function calls to speed up video encoding. It requires a DLL, which I will write in C (I have just a test one right now).
当我使用 cl编译DLL时/ I java-path / include / java-path / include / win32 -DL -ML Main.c -FeTest.dll
它可以编译,但是得到了32位DLL。在互联网上进行了一些研究之后,我发现我需要一个64位DLL。
When I compile the DLL with cl /I "java-path/include" /"java-path/include/win32" -DL -ML Main.c -FeTest.dll
it compiles, but I get a 32-bit DLL. After I did some research on the internet, I found out that I would need a 64-bit DLL instead.
经过更多研究,我发现这篇文章,这是唯一的一篇C语言(甚至很难找到C ++),但这仅在通过Visual Studio 2010进行编写/构建时才有效。我将Elipse用于Java,将CLion用于C,并通过开发人员命令提示符进行编译。所以这对我不起作用。如何将其重新编译为64位DLL?
After more research, I have found this post which is the only one for C (even C++ was hard to find), but this only works if you are writing/building via Visual Studio 2010. I am using Elipse for the Java, CLion for the C, and compiling via the "Developer Command Prompt." so this does not work for me. How might I recompile as a 64-bit DLL?
编辑:我正在使用随附的 cl.exe
使用Visual Studio 2017
I am using the cl.exe
that comes with Visual Studio 2017
更新:我在下找到了64位的
,但是在运行它时,我收到一个错误,指出库机器类型(x86)与目标类型(x64)冲突。如何更改库计算机的类型? cl.exe
C:\Program Files(x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\bin\Hostx64\x64\cl.exe
UPDATE: I found the 64-bit cl.exe
under C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\bin\Hostx64\x64\cl.exe
, however when running it, I get an error that the library machine type (x86) conflicts with the target type (x64). How do I change the library machine type?
推荐答案
正如我在[SO]:如何构建libjpeg 9b的DLL版本? (@CristiFati的答案)(来自 1。准备地面部分的项目符号),有多种方法可以处理 VStudio 中的命令行构建。
我将重点放在 vcvarsall.bat 。有关 [MSDN]:设置路径和命令行构建的环境变量( VStudio2017 链接的 VStudio2015 断开了)。
我准备了一个虚拟示例。
As I explained at the beginning of [SO]: How to build a DLL version of libjpeg 9b? (@CristiFati's answer) (bullets from 1. Prepare the ground section), there are different ways to deal with building from command line in VStudio.
I'm going to focus on vcvarsall.bat. More details on [MSDN]: Setting the Path and Environment Variables for Command-Line Builds (It's VStudio2015 as VStudio2017 link is broken).
I prepared a dummy example.
code.c :
#include <stdio.h>
#include "jni.h"
__declspec(dllexport) int func() {
JavaVMInitArgs args;
printf("Pointer size: %lld bits\n", sizeof(void*) * 8);
printf("JNI_GetDefaultJavaVMInitArgs returned: %d\n", JNI_GetDefaultJavaVMInitArgs(&args));
return 0;
}
构建:
e:\Work\Dev\StackOverflow\q050164687>"c:\Install\x86\Microsoft\Visual Studio Community\2017\VC\Auxiliary\Build\vcvarsall.bat" amd64
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.6.6
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
e:\Work\Dev\StackOverflow\q050164687>dir /b
code.c
e:\Work\Dev\StackOverflow\q050164687>cl /nologo /LD /I"c:\Install\x64\Oracle\Java\jdk1.8.0_152\include" /I"c:\Install\x64\Oracle\Java\jdk1.8.0_152\include\win32" /DWIN64 /DWIN32 code.c /link /LIBPATH:"c:\Install\x64\Oracle\Java\jdk1.8.0_152\lib" /OUT:dummy.dll jvm.lib
code.c
Creating library code.lib and object code.exp
e:\Work\Dev\StackOverflow\q050164687>dir /b
code.c
code.exp
code.lib
code.obj
dummy.dll
注释:
- 我的 vcvarsall 路径是自定义的,因为我安装了< C:\Install\x86\Microsoft\Visual Studio Community\2017 下的em> VStudio2017 。默认路径为 %SystemDrive%\程序文件(x86)\Microsoft Visual Studio\2017\社区
- 运行 vcvarsall后,我不必指定 cl.exe (或 link.exe ):
- 完整路径
- 构建选项(特定于体系结构,包括路径)
- 我仍然必须指定它没有的内容知道(例如 Java 的东西)
- My vcvarsall path is custom, because I installed VStudio2017 under "C:\Install\x86\Microsoft\Visual Studio Community\2017". Default path is "%SystemDrive%\Program Files (x86)\Microsoft Visual Studio\2017\Community"
- After running vcvarsall, I don't have to specify to cl.exe (or link.exe):
- The full path
- Build options (architecture specific, including paths)
- I still have to specify things that it doesn't know about (like Java stuff)
e:\Work\Dev\StackOverflow\q050164687>set PATH=%PATH%;c:\Install\x64\Oracle\Java\jdk1.8.0_152\jre\bin\server e:\Work\Dev\StackOverflow\q050164687>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import ctypes >>> dummy = ctypes.CDLL("dummy.dll") >>> dummy.func() Pointer size: 64 bits JNI_GetDefaultJavaVMInitArgs returned: -1 0 >>>
这篇关于如何编译用C编写的64位dll?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!