Linux 系统编程总复习

 结构图

一、make的用法 

代码:

rationalapp.cpp

#include<iostream>
#include"rational.h"
int main(int argc,char* argv[]){
	Rational r(9,12);
	std::cout<<r<<std::endl;
	return 0;

}

 rational.h

#include<iostream>
class Rational {
public:
	Rational(int num, int den);
	friend std::ostream& operator<<(std::ostream& out,const Rational& r);

private:
	int numerator,denominator;

};

rational.cpp 

#include"rational.h"
#include"tool.h"
Rational::Rational(int num,int den):numerator(num/gcd(num,den)),denominator(den/gcd(num,den)){}

std::ostream& operator <<(std::ostream& out,const Rational& r){
	out<<r.numerator<<"/"<<r.denominator;
	return out;
}

tool.h

int gcd(int m,int n);

tool.cpp 

#include"tool.h"

int gcd(int m,int n){
	while(n!=0){
		int t=m;
		m=n;
		n=t%n;
	}
	return m;

}

rat.mk

rationalapp:rationalapp.o rational.o tool.o
        g++ -o rationalapp rationalapp.o rational.o tool.o
rationalapp.o: rationalapp.cpp
        g++ -c rationalapp.cpp
rational.o: rational.cpp
        g++ -c rational.cpp
tool.o:tool.cpp
        g++ -c tool.cpp

clean:
        rm *.o

结果: 

 二、进程间的通信

代码:

myforks.cpp

#include<iostream>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<string.h>

const int LEN=4096;
void han_int(int signo){
	pid_t child;
	while((child=waitpid(-1,NULL,WNOHANG))>0){
		std::cerr<<child<<" finish\n";
	}

}

int main(int argc,char* argv[]){
	signal(SIGCHLD,han_int);
	int fd[2];
	pipe(fd);
	pid_t child=fork();
	if(child == 0){
		close(fd[0]);
		char buf[LEN]={'\0'};
		strncpy(buf,argv[1],strlen(argv[1]));
		write(fd[1],buf,strlen(buf));
		//execlp("pwd","pwd",NULL);
		//execlp(argv[1],argv[1],NULL);
		exit(0);	
	}else{
		close(fd[1]);
		char buf[LEN]={'\0'};
		read(fd[0],buf,LEN);
		std::cout<<buf<<std::endl;
		while(true){
			
		}	
	
	}
	return 0;

}

结果:

三、线程间的通信 

代码:

mythead.cpp

#include<iostream>
#include<thread>

void myfun(char* s){
	//std::cout<<"thread test\n";
	std::cout<<s<<std::endl;
	
}
int main(int argc,char* argv[]){
	std::thread t(myfun,argv[1]);
	t.join();
	//t.detach();
	return 0;

}

结果: 

四、gcc和g++

安装

sudo apt install gcc
sudo apt install g++

编译程序

//最简单的编译方式:由于命令行中未指定可执行程序的文件名,编译器采用默认的 a.out
编译:
gcc hello.c
g++ hello.cpp

执行:
./a.out
Hello, world!


通常我们使用 -o 选项指定可执行程序的文件名,以下实例生成一个 helloworld 的可执行文件:
编译:
gcc hello.c -o hello
g++ hello.cpp -o hello
或者
gcc -o hello hello.c
g++ -o hello hello.cpp  

执行:
./hello
Hello, world!


如果是多个 C++ 代码文件,如 runoob1.cpp、runoob2.cpp,编译命令如下:
编译:
gcc runoob1.c runoob2.c -o runoob
g++ runoob1.cpp runoob2.cpp -o runoob

执行:
./runoob

//生成目标文件(后缀为.o)
gcc -c hello.c
g++ -c hello.cpp

生成hello.o

注意 

//gcc运行c++程序
gcc hello.cpp -lstdc++ -o hello
//g++运行c程序
g++ hello.c -o hello 

 参考博客:gcc与g++的区别_wsqyouth的博客-CSDN博客_g++ gcc

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值