Chromium base库很丰富,里面包含了google对Chromium的架构思想,很多东西可以学习学习。为了能完美的阅读代码,把libbase.a用起来,自己写写小例子GDB一把,对理解代码有如虎添翼的功效。
怎么架起来,看以下小例子:
#include "build_config.h"
#include "at_exit.h"
#include "bind.h"
void exit_first(void *data) { LOG(INFO) << "first"; }
void exit_last(void *data) { LOG(INFO) << "second"; }
void exit_task(void *data) { LOG(INFO) << "use bind"; }
class task {
public:
void func() { LOG(INFO) << "task::func"; }
};
int main(int argc, char**argv) {
base::AtExitManager manager; //这个对象在析构的时候,会去回调注册的callback
task tsk;
base::AtExitManager::RegisterCallback(&exit_first, nullptr); //注册callback
base::AtExitManager::RegisterCallback(&exit_last, nullptr);
//指出注册base::Bind绑定的callback
base::AtExitManager::RegisterTask(base::Bind(&exit_task, nullptr));
base::AtExitManager::RegisterTask(base::Bind(&task::func, base::Unretained(&tsk)));
// This method can take the initiative to register callback functions
//base::AtExitManager::ProcessCallbacksNow(); //可以自己主动的调用
return 0;
}
编译命令:
g++ -g -O0 main.cpp -o main -std=c++11 -fno-stack-protector
-I src/base -I src/ -I src/build
-lbase -lbase_static -lpthread -lsymbolize
-L src/out/Debug/obj/base/
GDB时注意,可能base库的源代码找不到(../../base/at_exit.cc: 没有那个文件或目录.),要加入以下语句
set substitute-path ../../ Chromium源代码绝对目录