linux c++编译 与调试

log.h

 

#ifndef LOG_H.H
#define LOG_H.H
#include <fstream>
#include <string>
class CLLog
{
public:
        CLLog();
        ~CLLog();
        void SetFilePath(char* cFilePath);
        void GetFilePath(char* cFilePath);
        void Log(char* cLogString);
        bool Init();
private:
        std::fstream    fFile;
        std::string sFilePath;
        std::string sContent;
};
#endif

 

log.cpp

 

#include <time.h>
#include "log.h"
#include <ios>
#include <iostream>
CLLog::CLLog()
{
        sFilePath = "";
}
CLLog::~CLLog()
{
        if(fFile)
        {
                fFile.close();
        }
        sFilePath = "";

}
bool  CLLog::Init()
{
        fFile.open(sFilePath.c_str() , std::ios::in|std::ios::out|std::ios::app);
        if(!fFile)
        {
                std::cout<<"打开-"<<sFilePath<<"-文件失败!"<<std::endl;
                return false;
        }
        std::cout<<"打开文件成功!"<<std::endl;
        return true;
}
void CLLog::SetFilePath(char* cFilePath)
{
        sFilePath = std::string(cFilePath);
        Init();
}
void CLLog::GetFilePath(char* cFilePath)
{
        //strcpy(cFilePath , sFilePath.c_str());
        sFilePath.copy(cFilePath , sFilePath.length(),0);
}
void CLLog::Log(char* cLogString)
{
        time_t t = time(0);
        char *cTmp=new char[32]();
        strftime(cTmp ,32,"%Y-%m-%d %H:%M:%S",localtime(&t));

        sContent = std::string(cLogString)+"/t"+std::string(cTmp) +"/t/n";
        delete cTmp;
        cTmp = NULL;
        fFile.seekp(0,std::ios_base::end);
        fFile.write(sContent.c_str() , sContent.length());
}

 

main.cpp

 

#include "log.h"
#include <iostream>
int main()
{
        int i= 8;
        int j = i;
        CLLog log;
        log.SetFilePath("log.txt");
        for(int i=0;i<50;i++)
                log.Log("adlkfjaldfjalsdf");
        std::cout<<"safdadf"<<std::endl;
        return 0;
}

 

编译:

1 . g++ log.cpp main.cpp -o main  

这种编译方法不能用于GDB调试,要想调试的加上 -g

g++ -g log.cpp main.cpp -o main

 

2.编译通过 可以进行调试(依次进行一下操作)

gdb main  //进入调试状态

 

break    6     //在main.cpp中的第6行设置断点,

break  log.cpp:37 // 在log.cpp的37行设置断点


run  运行

 

停止到断点出 

print    变量       // 打印变量值

step   //单步执行,可进入到函数体内

next   //单步执行,但不可进入到函数体内

continue  //从断点出继续执行

 

3 makefile

 

main:main.o log.o
        g++   -o main  main.o log.o  //前边一定要有一个tab键
log.o:log.cpp
        g++  -c log.cpp -g log.cpp
main.o:main.cpp
        g++  -c  main.cpp -g main.cpp

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章