C++命令空间使用和编译

先创建目录结构 src bin obj lib include

1.创建命名空间

创建一个头文件include/head.h;

#ifndef _GOOD_H
#define _GOOD_H
namespace stdtest
{
    extern int val;
    void func();
}
#endif

2.使用自定义名义空间

创建一个头文件src/main.cpp

 1 #include <iostream>
 2 #include "head.h"
 3 //调用命令空间
 4 using namespace stdtest;
 5 using namespace std;
 6 int stdtest::val=120;
 7 void stdtest::func(){
 8     cout <<"my namespace is stdtest."<<endl;
 9 }
10 int main(){
11     cout <<"hello word" << endl;
12     //函数内部调用命令空间
13     //using namespace stdtest;
14     cout <<"stdtest val="<<val<<endl;
15     stdtest::func();
16 }

3.用g++命令编译程序;生成可执行文件 放到 bin/hellword

 g++ -Wall -o  bin/hellword src/main.cpp -Iinclude