linux调用系统函数?如何调用库函数
其实linux调用系统函数的问题并不复杂,但是又很多的朋友都不太了解如何调用库函数,因此呢,今天小编就来为大家分享linux调用系统函数的一些知识,希望可以帮助到大家,下面我们一起来看看这个问题的分析吧!
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操作系统API调用syscall
标准的c函数库是所有的编译都要具有的函数库,(实际上还是略有不同),但是这些基本上实现方法略有不同,但是结果和标准是一样的。但是linux的系统调用,调用是linux的系统库,比如说unistd.h下的fork这个是Linux下特有,你在vs上,就没有这个库,也没有这个函数。同样在vs上写c,你可以引入头文件比如windows.h,显然这个库是Linux不具有的。简单说系统调用库根据具体的操作系统环境不同而不同,而c标准库,是所有支持c语言编译器都有的。
linux内核是什么意思(有啥作用)
安卓手机的内核是什么意思详细点
手机内核也是Linux内核,Android系统是基于Linux研发的,它负责管理系统的进程,内存,设备驱动程序,文件和网络系统,决定着系统的性能和稳定性,内核以独占的方式执行最底层任务,保证系统正常运行,协调多个并发进程,管理进程使用的内存,使它们相互之间不产生冲突,满足进程访问磁盘的请求等等.
linux内核是什么,有啥作用
(Linux)内核是(Linux)操作系统的核心,一般包含五大部分:进程管理、存储管理、文件管理、设备管理和网络管理,是一组程序模块,具有访问硬件设备和所有主存空间的权限,是仅有的能够执行特权指令的程序。主要功能是:资源抽象、资源分配、资源共享。(资源是指CPU、内存等。)在内核基础上挂载第三方软件便构成操作系统,Ubuntu、RedHat、Fedora、Debian等都是基于Linux内核(版本号可能不同)的不同操作系统。内核函数对用户是完全透明的,用户想要调用内核函数只有两种途径:一是应用程序→系统调用(程序接口)→操作系统;二是操作命令→系统程序(作业接口)→操作系统。内核是不是指的就是用shell控制的东西?shell命令可能是普通的应用程序,也可能是库函数或系统调用(你可以理解为内核函数)。如果你想查看某命令是普通shell命令,还是库函数或系统调用,可以在终端输入“man命令”查看。如manopen,左上角应该是OPEN(2)。1表示普通shell命令,2表示系统调用,3表示库函数。注:库函数事实上是内核函数的封装,介于应用程序与内核函数之间。应用程序是不能直接访问内核函数的,必须通过库函数。这是一种保护内核函数的一种机制。事实上很多时候我们都要用到内核函数,只是我们并不知道而已,如打开文件要调用open()、关闭文件要调用close()等等
linux内核是代码吗
当然是代码了,除了c语言就是汇编
linux原始内核是什么
Linux内核无疑是Linux操作系统的核心。它由以下五个子系统构成
(1)进程调度
(2)内存管理
(3)虚拟文件系统
(4)网络接口
(5)进程之间的通信