简体   繁体   English

如何从Linux上的Java代码调用C函数

[英]How do I call C functions from Java code on Linux

I am writing a Java program on Suse Linux 11 using JavaSE-1.6 and I am having a problem building using javac. 我正在使用JavaSE-1.6在Suse Linux 11上编写Java程序,我在使用javac时遇到了问题。

I am following the tutorial on 我正在关注教程

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html

and have so far written the following: 到目前为止写了以下内容:

package com.ctest;

class CTest
{
    // Native method declaration
    native int testCall();

    // Load the library
    static
    {
        System.loadLibrary("fpdpReaderLib");
    }

    public static void main(String args[])
    {
        int retVal;

        // Create class instance
        CTest cLangTest = new CTest();

        // Call native method
        retVal = cLangTest.testCall();

        System.out.println(retVal);
    }
}

When I run javac CTest.java i get an error: 当我运行javac CTest.java时,我收到一个错误:

/usr/lib/gcc/i586-suse-linux/4.3/../../../crt1.o: in function '_start':
/usr/src/packages/BUILD/glibc-2.9/csu/../sysdeps/i386/elf/start.S:115: undefined reference to 'main'
/tmp/cc97kcJu.o:(.data+0x28) undefined reference to 'hidden alias for int com::ctest::CTest::testCall()'
/tmp/cc97kcJu.o:(.data+0x74) undefined reference to 'hidden alias for int com::ctest::CTest::testCall()'
collect2: ld returned 1 exit status

I am suspecting it is using gcc rather than the java version of javac but I'm not sure. 我怀疑它是使用gcc而不是java版本的javac,但我不确定。

Any ideas what the problem could be? 任何想法可能是什么问题?

I have tried using the "--main=" option mentioned here: 我尝试过使用这里提到的“--main =”选项:

http://gcc.gnu.org/java/faq.html#4_1 http://gcc.gnu.org/java/faq.html#4_1

but instead of the error before I now just get: 但在我现在得到之前,而不是错误:

/tmp/ccwfugWq.o:(.data+0x28) undefined reference to 'hidden alias for int com::ctest::CTest::testCall()'
/tmp/ccwfugWq.o:(.data+0x74) undefined reference to 'hidden alias for int com::ctest::CTest::testCall()'
collect2: ld returned 1 exit status

From the page that you cited: 从您引用的页面:

The library containing the native code implementation is loaded by a call to System.loadLibrary(). 包含本机代码实现的库通过调用System.loadLibrary()加载。 Placing this call in a static initializer ensures this library is only loaded once per class. 将此调用放在静态初始化程序中可确保每个类仅加载一次此库。 The library can be loaded outside of the static block if your application requires it. 如果应用程序需要,可以在静态块之外加载库。 You might need to configure your environment so the loadLibrary method can find your native code library. 您可能需要配置环境,以便loadLibrary方法可以找到您的本机代码库。

My emphasis. 我的重点。 Have you set the LD_LIBRARY_PATH (or whatever is appropriate) for your system? 您是否为系统设置了LD_LIBRARY_PATH(或任何适当的)?

I suggest you run which javac to determine which compiler you are using. 我建议你运行which javac来确定你正在使用哪个编译器。 If you want Java 6, you can't use gcj. 如果你想要Java 6,你就不能使用gcj。 You need to fix you PATH so you have using a javac from JDK 6. 您需要修复PATH,以便使用JDK 6中的javac

I think you should install and use the Sun Java SDK rather than using the gcc javac compiler. 我认为您应该安装和使用Sun Java SDK而不是使用gcc javac编译器。

Google for suse javac gcc throws up loads of similar problems and the solution always seems to be to use the Sun JDK. Google for suse javac gcc引发了大量类似的问题,解决方案似乎总是使用Sun JDK。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM