linux 函数 Linux软件大全

大家好,关于linux 函数很多朋友都还不太明白,今天小编就来为大家分享关于Linux软件大全的知识,希望对各位有所帮助!

Linux库函数,Linux 系统API 这两个概念有区别吗

Linux下对文件操作有两种方式:系统调用(system call)和库函数调用(Library functions)。系统调用实际上就是指最底层的一个调用,在linux程序设计里面就是底层调用的意思。面向的是硬件。而库函数调用则面向的是应用开发的,相当于应用程序的api,采用这样的方式有很多种原因,第一:双缓冲技术的实现。第二,可移植性。第三,底层调用本身的一些性能方面的缺陷。第四:让api也可以有了级别和专门的工作面向。

1、系统调用

系统调用提供的函数如open, close, read, write, ioctl等,需包含头文件unistd.h.以write为例:其函数原型为 size_t write(int fd, const void*buf, size_t nbytes),其操作对象为文件描述符或文件句柄fd(file descriptor),要想写一个文件,必须先以可写权限用open系统调用打开一个文件,获得所打开文件的fd,例如 fd=open(\逗/dev/video\地, O_RDWR)。fd是一个整型值,每新打开一个文件,所获得的fd为当前最大fd加1.Linux系统默认分配了3个文件描述符值:0-standard input,1-standard output,2-standard error.

系统调用通常用于底层文件访问(low-level file access),例如在驱动程序中对设备文件的直接访问。

系统调用是操作系统相关的,因此一般没有跨操作系统的可移植性。

系统调用发生在内核空间,因此如果在用户空间的一般应用程序中使用系统调用来进行文件操作,会有用户空间到内核空间切换的开销。事实上,即使在用户空间使用库函数来对文件进行操作,因为文件总是存在于存储介质上,因此不管是读写操作,都是对硬件(存储器)的操作,都必然会引起系统调用。也就是说,库函数对文件的操作实际上是通过系统调用来实现的。例如C库函数fwrite()就是通过write()系统调用来实现的。

这样的话,使用库函数也有系统调用的开销,为什么不直接使用系统调用呢看这是因为,读写文件通常是大量的数据(这种大量是相对于底层驱动的系统调用所实现的数据操作单位而言),这时,使用库函数就可以大大减少系统调用的次数。这一结果又缘于缓冲区技术。在用户空间和内核空间,对文件操作都使用了缓冲区,例如用fwrite写文件,都是先将内容写到用户空间缓冲区,当用户空间缓冲区满或者写操作结束时,才将用户缓冲区的内容写到内核缓冲区,同样的道理,当内核缓冲区满或写结束时才将内核缓冲区内容写到文件对应的硬件媒介。

2、库函数调用

标准C库函数提供的文件操作函数如fopen, fread, fwrite, fclose, fflush, fseek等,需包含头文件stdio.h.以fwrite为例,其函数原型为size_t fwrite(const void*buffer, size_t size, size_t item_num, FILE*pf),其操作对象为文件指针FILE*pf,要想写一个文件,必须先以可写权限用fopen函数打开一个文件,获得所打开文件的FILE结构指针pf,例如pf=fopen(\逗~/proj/filename\地,\逗w\地)。实际上,由于库函数对文件的操作最终是通过系统调用实现的,因此,每打开一个文件所获得的FILE结构指针都有一个内核空间的文件描述符fd与之对应。同样有相应的预定义的FILE指针:stdin-standard input,stdout-standard output,stderr-standard error.

库函数调用通常用于应用程序中对一般文件的访问。

库函数调用是系统无关的,因此可移植性好。

由于库函数调用是基于C库的,因此也就不可能用于内核空间的驱动程序中对设备的操作。

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函数是什么意思

Linux函数是什么意思?在Linux操作系统中,函数是一种可重用的代码块,它负责执行特定的任务并返回结果。Linux函数可以接受参数和返回值,以便在程序中传递信息和修改变量。这种代码复用的方式可以使程序更加模块化,并且减少代码的重复书写。

如何使用Linux函数?要使用Linux函数,您需要编写一个函数定义,包括函数名称、参数列表以及要执行的指令序列。然后,您可以在程序的主要部分中调用函数并传递参数。Linux提供了许多常用的函数,包括文件I/O、系统调用和字符串操作等。您可以使用这些标准函数,也可以编写自己的自定义函数来实现特定的任务。

Linux函数的优势是什么? Linux函数的优势是该函数的代码可以在程序中多次使用,以节省编程时间。此外,使用函数还可以增加代码的可读性和可维护性。由于它们的模块化设计,它们可以很容易地分类、测试和修改。Linux函数还有助于降低程序中的错误率,因为它们可以独立地进行测试和验证。因此,Linux函数是编写高质量可靠的系统的关键部分之一。

阅读剩余
THE END