程序分解以及Makefile 编写

开始时程序:

ngnsvr9 [** NONE **]/home/xionghailong/c++ $ cat orig.cpp
#include<iostream>

using namespace std;

struct Node
{
        Node *p_next;
        int value;
};

Node* addNode (Node* p_list, int value)
{
        Node *p_new_node = new Node;
        p_new_node->value = value;
        p_new_node->p_next = p_list;

        return p_new_node;
}

void printList(const Node* p_list)
{
        const Node* p_cur_node = p_list;
        while (p_cur_node != NULL)
        {
                cout<< p_cur_node->value<<endl;
                p_cur_node = p_cur_node->p_next;
        }
}

int main()
{
        Node *p_list = NULL;
        for(int i = 0; i < 10; ++i)
        {
                int value;
                cout<<"Enter value for list node: ";
                cin >> value;
                p_list = addNode(p_list, value);
        }
        printList(p_list);
}

当工程代码很大时,代码的可读性会大大降低;我们可以将代码分解为:linkedlist.cpp,linkedlist.h,orig.cpp

linkedlist.cpp:

ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ cat linkedlist.cpp
#include <iostream>
#include "linkedlist.h"

using namespace std;

Node* addNode (Node* p_list, int value)
{
        Node *p_new_node = new Node;
        p_new_node->value = value;
        p_new_node->p_next = p_list;

        return p_new_node;
}

void printList(const Node* p_list)
{
        const Node* p_cur_node = p_list;
        while (p_cur_node != NULL)
        {
                cout<< p_cur_node->value<<endl;
                p_cur_node = p_cur_node->p_next;
        }
}

linkedlist.h:

ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ cat linkedlist.h
struct Node
{
        Node *p_next;
        int value;
};

Node* addNode (Node* p_list, int value);

void printList(const Node* p_list);

orig.cpp:

void printList(const Node* p_list);ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ cat orig.cpp
#include <iostream>
#include "linkedlist.h"

using namespace std;

int main()
{
        Node *p_list = NULL;
        for(int i = 0; i < 10; ++i)
        {
                int value;
                cout<<"Enter value for list node: ";
                cin >> value;
                p_list = addNode(p_list, value);
        }
        printList(p_list);
}

可以直接用如下命令进行执行:

ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ g++ orig.cpp linkedlist.cpp
In file included from orig.cpp:2:
linkedlist.h:8:36: warning: no newline at end of file
orig.cpp:17:2: warning: no newline at end of file
In file included from linkedlist.cpp:2:
linkedlist.h:8:36: warning: no newline at end of file
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ ls
a.out  linkedlist.cpp  linkedlist.cpp.bak  linkedlist.h  linkedlist.h.bak  makefile.txt  makefile.txt.bak  orig.cpp  orig.cpp.bak
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ ./a.out
Enter value for list node: 2
Enter value for list node: 2
Enter value for list node: 2
Enter value for list node: 2
Enter value for list node: 23
Enter value for list node: 3
Enter value for list node: 3
Enter value for list node: 3
Enter value for list node: 3
Enter value for list node: 3
3
3
3
3
3
23
2
2
2
2

采用Makefile文件可以更加直接明了:

Makefile文件如下:

ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ cat makefile.txt
all: orig

orig: orig.o linkedlist.o
        g++  orig.o linkedlist.o -o orig
    
orig.o: orig.cpp
        g++ -c orig.cpp
    
linkedlist.o: linkedlist.cpp
        g++ -c linkedlist.cpp
    
clean:
        rm -rf *o orig


执行:

ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ make -f makefile.txt
g++ -c orig.cpp
In file included from orig.cpp:2:
linkedlist.h:8:36: warning: no newline at end of file
orig.cpp:17:2: warning: no newline at end of file
g++ -c linkedlist.cpp
In file included from linkedlist.cpp:2:
linkedlist.h:8:36: warning: no newline at end of file
g++  orig.o linkedlist.o -o orig
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ ls
a.out  linkedlist.cpp  linkedlist.cpp.bak  linkedlist.h  linkedlist.h.bak  linkedlist.o  makefile.txt  makefile.txt.bak  orig orig.cpp  orig.cpp.bak  orig.o
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ ./orig
Enter value for list node: 2
Enter value for list node: 2
Enter value for list node: 1
Enter value for list node: 2
Enter value for list node: 3
Enter value for list node: 1
Enter value for list node: 23
Enter value for list node: 4
Enter value for list node: 5
Enter value for list node: 56
56
5
4
23
1
3
2
1
2
2

生成了可执行文件orig


执行clean删除.o 和可执行文件

ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ make -f makefile.txt clean
rm -rf *o orig
ngnsvr9 [** NONE **]/home/xionghailong/c++/orig $ ls
a.out  linkedlist.cpp  linkedlist.cpp.bak  linkedlist.h  linkedlist.h.bak  makefile.txt  makefile.txt.bak  orig.cpp  orig.cpp.bak


希望对大家能有所帮助。














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值