Skip to content

Latest commit

 

History

History
executable file
·
54 lines (47 loc) · 1.74 KB

08-global_data.md

File metadata and controls

executable file
·
54 lines (47 loc) · 1.74 KB

代码总览

global_data.h和global_data.cc.

维护了一个类: GlobalData.

也是获取一些环境变量.

功能/知识

1. 类方法GlobalData::InitHostInfo()

  • 初始化一些东西吧.

  • 赋值host_ip_, 如果环境变量CYBER_IP有值, 把CYBER_IP赋给host_ip_; 如果果环境变量CYBER_IP无值, 当前设备IP赋给host_ip_. 只支持IPv4?

  • 测试gethostname的一个demo:

    #include <unistd.h>
    #include <iostream>
    #include <string>
    int main() {
      char host_name[1024];
      gethostname(host_name, sizeof(host_name)); //返回hostname,我的是carbon
      std::cout<<host_name<<std::endl;
      return 0;
    }
  • unistd.hLinux/Unix系统中内置头文件, 包含了许多系统服务的函数原型, 例如read函数 write函数和getpid函数等. 其作用相当于windows操作系统的windows.h, 是操作系统为用户提供的统一API接口, 方便调用系统提供的一些服务.

2. 方法program_path()

  • 返回当前程序的绝对路径.
  • /proc/self/exe是一个符号链接, 代表当前程序的绝对路径. 用readlink读取/proc/self/exe可以获取当前程序的绝对路径. 一个demo:
    #include <unistd.h>
    #include <iostream>
    int main(int argc, const char* argv[]) {
      char path[1025];
      auto len = readlink("/proc/self/exe", path, sizeof(path) - 1); 
      if (len == -1) {
        return 0;
      }
      path[len] = '\0';
      std::cout << path << std::endl; // /home/shiqiang/apollo/with_apollo/temp/a.out
      return 0;
    }
    会打印:
    shiqiang@carbon:~/apollo/with_apollo/temp$ ./a.out 
    /home/shiqiang/apollo/with_apollo/temp/a.out