C++ 的启动, 编译, 和实用工具

Args , compiling, and ssh example of C++

Posted by Eric on October 27, 2022

主要包括了几个重要的模板, 涉及启动参数, makefile, 和ssh 等. 在实际开发中效率提升非常明显.

1. 完美参数实例 rdp

参数任意位置和个数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cstdlib>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>

using namespace std;

int main(int argc, char ** argv){

    string ip;
    string user = "firefly";
    bool flag_fullscreen = false;
    
    for(int i=1;i<argc;i++)
    {       
        if(strcmp(argv[i],"f")==0)
        {
            flag_fullscreen = true;
        }
        else if(strcmp(argv[i],"-f")==0){
            flag_fullscreen = true;
        }
        else if(strcmp(argv[i],"r")==0){
            user = "root";
        }
        else if(strcmp(argv[i],"-r")==0){
            user = "root";
        }
        else{
            ip = argv[i];
        }
            
    }   

    string str = "xfreerdp /v:192.168.57."+ip+" /u:"+user+" /p:firefly";
    if (flag_fullscreen)
    {
        str.append(" -f");
    }

    system(str.c_str());

    return 0;
}

2. makefile批量生成.out模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# makefile

CC = g++
CPPFLAG = -Wall -g -std=c++11

TARGET = $(patsubst %.cpp, %.out, $(wildcard *.cpp))
HEADER = $(wildcard headers/*.h)

%.out : %.cpp $(HEADER)
        $(CC) $(CPPFLAG) $< -o $@

.PHONY : all clean

all : $(TARGET)

clean : 
        rm -f *.out

3. No pass

1
2
# vim /etc/ssh/ssh_config   
StrictHostKeyChecking no
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>

using namespace std;

int main(int argc, char ** argv){

    string ip = argv[1];
     
    string str = "sshpass -p firefly ssh firefly@192.168.57."+ip;

    system(str.c_str());

    return 0;
}