加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-13 22:51:22 所属栏目:Linux 来源:网络整理
导读:我正在使用 JavaSE-1.6在Suse Linux 11上编写Java程序,我在使用javac时遇到了问题. 我正在关注教程 http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html 到目前为止写了以下内容: package com.ctest;class CTest{ // Native m
我正在使用 JavaSE-1.6在Suse Linux 11上编写Java程序,我在使用javac时遇到了问题.

我正在关注教程

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

到目前为止写了以下内容:

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);
    }
}

当我运行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

我怀疑它是使用gcc而不是java版本的javac,但我不确定.

任何想法可能是什么问题?

我尝试过使用这里提到的“–main =”选项:

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

但在我现在得到之前,而不是错误:

/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

解决方法

我认为您应该安装和使用Sun Java SDK而不是使用gcc javac编译器.

Google for suse javac gcc引发了大量类似的问题,解决方案似乎总是使用Sun JDK.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读