linux文件调用,linux用户密码修改

大家好,linux文件调用相信很多的网友都不是很明白,包括linux用户密码修改也是一样,不过没有关系,接下来就来为大家分享关于linux文件调用和linux用户密码修改的一些知识点,大家可以关注收藏,免得下次来找不到哦,下面我们开始吧!

linux环境java如何调用so文件

用JNI实现

实例:

创建HelloWorld.java

class HelloWorld

{

private native void print();

public staticvoid main(String[] args)

{

new HelloWorld().print();

}

static

{

System.loadLibrary("HelloWorld");

}

}

注意print方法的声明,关键字native表明该方法是一个原生代码实现的。另外注意static代码段的System.loadLibrary调用,这段代码表示在程序加载的时候,自动加载libHelloWorld.so库。

编译HelloWorld.java

在命令行中运行如下命令:

javac HelloWorld.java

在当前文件夹编译生成HelloWorld.class。

生成HelloWorld.h

在命令行中运行如下命令:

javah-jni HelloWorld

在当前文件夹中会生成HelloWorld.h。打开HelloWorld.h将会发现如下代码:

/* 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

该文件中包含了一个函数Java_HelloWorld_print的声明。这里面包含两个参数,非常重要,后面讲实现的时候会讲到。

实现HelloWorld.c

创建HelloWorld.c文件输入如下的代码:

#include<jni.h>

#include<stdio.h>

#include"HelloWorld.h"

JNIEXPORT void JNICALL

Java_HelloWorld_print(JNIEnv*env, jobject obj)

{

printf("Hello World!\n");

}

注意必须要包含jni.h头文件,该文件中定义了JNI用到的各种类型,宏定义等。

另外需要注意Java_HelloWorld_print的两个参数,本例比较简单,不需要用到这两个参数。但是这两个参数在JNI中非常重要。

env代表java虚拟机环境,Java传过来的参数和c有很大的不同,需要调用JVM提供的接口来转换成C类型的,就是通过调用env方法来完成转换的。

obj代表调用的对象,相当于c++的this。当c函数需要改变调用对象成员变量时,可以通过操作这个对象来完成。

编译生成libHelloWorld.so

在Linux下执行如下命令来完成编译工作:

cc-I/usr/lib/jvm/java-6-sun/include/linux/

-I/usr/lib/jvm/java-6-sun/include/

-fPIC-shared-o libHelloWorld.so HelloWorld.c

在当前目录生成libHelloWorld.so。注意一定需要包含Java的include目录(请根据自己系统环境设定),因为Helloworld.c中包含了jni.h。

另外一个值得注意的是在HelloWorld.java中我们LoadLibrary方法加载的是

“HelloWorld”,可我们生成的Library却是libHelloWorld。这是Linux的链接规定的,一个库的必须要是:lib+库

名+.so。链接的时候只需要提供库名就可以了。

运行Java程序HelloWorld

大功告成最后一步,验证前面的成果的时刻到了:

java HelloWorld

如果你这步发生问题,如果这步你收到java.lang.UnsatisfiedLinkError异常,可以通过如下方式指明共享库的路径:

java-Djava.library.path='.' HelloWorld

当然还有其他的方式可以指明路径请参考《在Linux平台下使用JNI》。

我们可以看到久违的“Hello world!”输出了。

linux c++ 怎么 调用自己函数的

实验平台:ubuntu 12.04+ g++4.6+ matlab2012a

问题描述:

有一个c++程序main.cpp,和一个matlab函数myFunc.m。现在要做这件事:

1)从main.cpp中传递2个double类型的数值a和b到myFunc.m中

2)myFunc.m中求和(sum= a+b)

3)main.cpp中接收myFunc.m返回的和并输出。

思路:

1)设置matlab的编译器,使用gcc编译器。编译m文件成.so。

2)编译.cpp文件,编译时调用.so库(添加.so的路径到编译选项)。

步骤:

1)将自己的matlab函数(myFunc.m)编译成动态链接库

(1)设定编译器为gcc,在matlab命令行依次执行命令mex-setup和mbuild-setup:

>> mex-setup Options files control which compiler to use, the compiler and link command options, and the runtime libraries to link against. Using the'mex-setup' command selects an options file that is placed in~/.matlab/R2012a and used by default for'mex'. An options file in the current working directory or specified on the command line overrides the default options file in~/.matlab/R2012a. To override the default options file, use the'mex-f' command(see'mex-help' for more information).The options files available for mex are: 1:/opt/MATLAB/R2012a/bin/mexopts.sh: Template Options file for building gcc MEX-files 0: Exit with no changesEnter the number of the compiler(0-1):1/opt/MATLAB/R2012a/bin/mexopts.sh is being copied to~/.matlab/R2012a/mexopts.shcp: cannot create regular file `~/.matlab/R2012a/mexopts.sh': Permission denied************************************************************************** Warning: The MATLAB C and Fortran API has changed to support MATLAB variables with more than 2^32-1 elements. In the near future you will be required to update your code to utilize the new API. You can find more information about this at: Building with the-largeArrayDims option enables the new API.**************************************************************************>> mbuild-setup Options files control which compiler to use, the compiler and link command options, and the runtime libraries to link against. Using the'mbuild-setup' command selects an options file that is placed in~/.matlab/R2012a and used by default for'mbuild'. An options file in the current working directory or specified on the command line overrides the default options file in~/.matlab/R2012a. To override the default options file, use the'mbuild-f' command(see'mbuild-help' for more information).The options files available for mbuild are: 1:/opt/MATLAB/R2012a/bin/mbuildopts.sh: Build and link with MATLAB Compiler generated library via the system ANSI C/C++ compiler 0: Exit with no changesEnter the number of the compiler(0-1):1/opt/MATLAB/R2012a/bin/mbuildopts.sh is being copied to~/.matlab/R2012a/mbuildopts.shcp: cannot create regular file `~/.matlab/R2012a/mbuildopts.sh': Permission denied>>

(2)在matlab中,编写myFunc.m文件内容如下:

function [ C ]= myFunc(A, B)C= A+B;end

(3)生成myFunc.m的动态链接库(.so)

>> mcc-W cpplib:libmyFunc-T link:lib myFunc.m-cWarning: MATLAB Toolbox Path Cache is out of date and is not being used.Type'help toolbox_path_cache' for more info>>

等待数秒,完成。可以看到myFunc.m所在的目录下生成了多个文件:

$ lslibmyFunc.cpp libmyFunc.ctf libmyFunc.exports libmyFunc.h libmyFunc.so main.cpp mccExcludedFiles.log myFunc.m readme.txt

令解释:其中-W是控制编译之后的封装格式;cpplib是指编译成C++的lib;cpplib冒号后面是指编译的库的名字;-T表示目

标,link:lib表示要连接到一个库文件的目标,目标的名字即是.m函数的名字。-c表明需要生成.ctf文件,比如本例如果不加-c就不会生成

“libmyFunc.ctf”。

生成的文件中打开“libmyFunc.h”可以看到一行:

extern LIB_libmyFunc_CPP_API void MW_CALL_CONV myFunc(int nargout, mwArray& C, const mwArray& A, const mwArray& B);

个就是我们的myFunc.c函数待会儿在c++中调用时的接口。有4个参数,第一个是参数个数,第二个是用来接收函数返回值的,后面2个是从c++中传

递进来的变量。我们还会用到“libmyFunc.h”中的另外2个函数:libmyFuncInitialize()初始化,和注销

libmyFuncTerminate()。

2)编写main.cpp,代码如下:

#include<iostream>#include"mclmcr.h"#include"matrix.h"#include"mclcppclass.h"#include"libmyFunc.h"#include"mclmcrrt.h"using namespace std;int main(){// initialize lib,这里必须做初始化! if(!libmyFuncInitialize()){ std::cout<<"Could not initialize libmyFunc!"<< std::endl; return-1;}//用户输入2个数值 double a, b; cout<<"Please input 2 numbers<a b> and then press enter:"<<endl; cin>> a; cin>> b; double c;//used to receive the result//为变量分配内存空间, maltab只有一种变量,就是矩阵,为了和c++变量接轨,设置成1*1的矩阵 mwArray mwA(1, 1, mxDOUBLE_CLASS);//1,1表示矩阵的大小, mxDOUBLE_CLASS表示变量的精度 mwArray mwB(1, 1, mxDOUBLE_CLASS); mwArray mwC(1, 1, mxDOUBLE_CLASS);//调用类里面的SetData函数给类赋值 mwA.SetData(&a, 1); mwB.SetData(&b, 1);//调用自己的函数,求和。 myFunc(1, mwC, mwA, mwB); c= mwC.Get(1, 1); cout<<"The sum is:"<<c<<endl;//后面是一些终止调用的程序// terminate the lib libmyFuncTerminate();// terminate MCR mclTerminateApplication(); return EXIT_SUCCESS;}

3)编译main.cpp函数,调用libmyFunc.so

$ g++-o main-I.-I/opt/MATLAB/R2012a/extern/include-L.-L/opt/MATLAB/R2012a/runtime/glnxa64 main.cpp-lmyFunc-lm-lmwmclmcrrt-lmwmclmcr

开始编译时也遇到一些问题,主要是链接库路径问题导致的编译错误,详细错误汇总在另篇笔记里(点此查看)。

编译成功后,需要装一下MCR Installer,步骤点此。

运行结果:

$./main Warning: latest version of matlab app-defaults file not found.Contact your system administrator to have this file installedPlease input 2 numbers<a b> and then press enter: 10 100The sum is: 110$./main Warning: latest version of matlab app-defaults file not found.Contact your system administrator to have this file installedPlease input 2 numbers<a b> and then press enter: 1 1The sum is: 2

源代码及编译过程中的所有文件已打包,点击这里可以下载。

linux下如何运行可执行文件

1、打开kali linux的终端。创建一个文件并命名为dutest.c。在终端输入:touch test.c。

2、可以看到已经生成了一个后缀为test.c的源文件。然后用vim工具打开这个文件并编写代码。在终端中输入:vim test.c或者gvim test.c打开这个文件并编写代码。

3、编写完了这个代码。现在开始编译源文件。在终端中输入:gcc test.cgcc是linux自带的c语言编译器。如果是windows则要用ide工具来编译。linux系统一般写C语言用gcc+vim+gdb三个自带的工具就可以了。

4、打完gcc test.c编译完C源文件。然后就可以看见a.out的文件。一般linux系统就默认为a.out为编译完的文件。现在运行a.out文件。在a.out文件的目录下打开终端并输入./a.out就是运行文件了。

5、如果想要编译完的文件名不要用a.out文件。就可以在编译时打入.gcc test.c-o test.out然后就可以看见有一个test.out.文件了。-o后面跟着的编译生成的文件名。

6、再运行test.out在终端中输入./test.out结果如图。这样在linux系统下编译并运行C语言就完成了。

阅读剩余
THE END