• 2007-11-11

    bcc的多文件编译

     经过摸索,我终于能在VIM下用bcc来编译多文件的C\C++程序了.
      搜了好多的文章才好不容易看到了bcc的makefile的格式!看到一篇文章后明白,要想做到构建多文件程序,有两个方法,一个就是多数IDE那样建立工程文件,再就是makefile了,前面那个工程文件想必用过TC和VC的人都知道,makefile我也知道,只是从来没有写过,更别说用过了!
       那是上次心血来潮想要试试linux的时候在图书馆借了一本LINUX下的C编程的书,上面提到了makefile这个东西,对linux来说,它是很重要的!
       下面是我的实例
       这个实例是一全stack类。

                              class.cpp   -- 类的实现文件

    #include <iostream.h>
    #include "class.h"

    void stack::init() {top = -1;}

    void stack::push(int n)
     {
      if (isFull())
      {
       errMsg("Full stack.Can't push.");
       return;
      }
      arr[++top] = n;
     }

    int stack::pop()
     {
      if (isEmpty())
      {
       errMsg("Empty stack.popping dummy value.");
       return dummy_val;
      }
      return arr[top--];
     }

    bool stack::isEmpty() {return top<0;}

    bool stack::isFull() {return top>=MaxStack-1;}

    void stack::dump()
     {
      cout << "Stack contents,top to button:\n";
      for (int i = top;i >= 0; i--)
       cout << '\t' << arr[i] << '\n';
     }


    void stack::errMsg(const char* msg) const
     {
      cerr << "\n*** Stack operation failure: " << msg << '\n';
     }

     


                                    class.h     -- 类的声明文件

    #ifndef CLASS_H
    #define CLASS_H
    #include <iostream.h>


    class stack
    {
    public:
     enum {MaxStack = 5};

     void init();
     void push(int n);
     int pop();


     bool isEmpty();

     bool isFull();

     void dump();
    private:
     void errMsg(const char* msg) const;
     int top;
     int arr[MaxStack];
     int dummy_val;
    };
    #endif

     


                                        stack.cpp   -- main

    #include "class.h"
    #include <iostream.h>

    int main()
    {
     stack s;
     s.init();
     s.push(9);
     s.push(4);
     s.dump();
     cout<<"poping "<<s.pop()<<'\n';
     s.dump();
     s.push(8);
     s.dump();
     s.pop();
     s.pop();
     s.dump();
     s.push(3);
     s.push(5);
     s.dump();
     for (unsigned i = 0; i< stack::MaxStack;i++)
      s.push(1);
     s.dump();
     return 0;
    }
     


    下面就是重点了,很多人没有接触过make嘛 ,
                              ngn.mak  -- makefile 文件
    stack.exe : stack.obj class.obj
     bcc32 stack.obj class.obj
    stack.obj : class.h  stack.cpp
     bcc32 -c stack.cpp
    class.obj : class.cpp class.h
     bcc32 -c class.cpp


    写完上面的四个文件后,在vim中运行 ::!nmake ngn.mak
                                         :!stack


    结果就这样出来了!   
               


    历史上的今天:


    收藏到:Del.icio.us