VS2010在尝试编译基本JNI测试代码时出错

Error in VS2010 when trying to compile basic JNI Test Code

本文关键字:测试 JNI 代码 出错 编译 VS2010      更新时间:2023-10-16

我正在尝试编译一个基本的JNI测试,为一个DLL(带有头和lib文件)做准备,我希望在未来几天内收到它,我需要为它创建一个JNI接口。

我试着把它指向JNI.lib的x64/x86库。我试着根本不把它指向库。我已经尝试将其指向include目录的两个x64/x86版本,但总是出现如下错误。

Visual Studio给了我以下错误,我不知道为什么:

1>------ Build started: Project: CLibHelloWorld, Configuration: Debug Win32 ------
1>Build started 04/03/2015 19:04:09.
1>InitializeBuildStatus:
1>  Creating "DebugCLibHelloWorld.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1>  CLibHelloWorld.c
1>Link:
1>     Creating library F:Work4-CompaniesDigitalARCSystemsKlearKaptureCameraInterfacetesttest-jniDebugCLibHelloWorld.lib and object F:Work4-CompaniesDigitalARCSystemsKlearKaptureCameraInterfacetesttest-jniDebugCLibHelloWorld.exp
1>  test-jni.vcxproj -> F:Work4-CompaniesDigitalARCSystemsKlearKaptureCameraInterfacetesttest-jniDebugCLibHelloWorld.dll
1>FinalizeBuildStatus:
1>  Deleting file "DebugCLibHelloWorld.unsuccessfulbuild".
1>  Touching "DebugCLibHelloWorld.lastbuildstate".
1>
1>Build succeeded.
1>
1>Time Elapsed 00:00:00.43
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

HelloWorld.java(使用javacHelloWorld.jav编译):

class HelloWorld {
    public native void print();  //native method
    static   //static initializer code
    {
        System.loadLibrary("CLibHelloWorld");
    } 
    public static void main(String[] args)
    {
        HelloWorld hw = new HelloWorld();
        hw.print();
    }
}

HelloWorld.h(由javah-jniHelloWorld生成):

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HelloWorld
 * Method:    print
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloWorld_print
  (JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif

CLibHelloWorld.c:

#include "HelloWorld.h"
#include "jni.h"
#include  "stdio.h"
JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
    printf("Hello worldn");
    return;
}

将注释转换为答案,因为这显然有助于解决问题。

调查MSBuild问题的一种方法是查看详细的日志。从VS命令提示符,cd到项目的目录,然后执行命令msbuild MyProject.vcxproj /v:d。这将建立详细的详细程度。在输出中搜索链接器命令,您应该会看到类似的内容

C:Program Files (x86)Microsoft Visual Studio 12.0VCbinlink.exe /OUT:"c:my_output_pathmyfile.dll" ...

链接器的/OUT选项就是您想要的。如果根本没有调用链接器,那么您有一些设置不需要为项目生成二进制文件。