c++ 链接
header.h
#ifndef HEADER_H #define HEADER_H unsigned long getFac(unsigned short num); static const unsigned short headerNum = 5; #endif // HEADER_H_INCLUDED
this.cpp
#include "header.h" #include <iostream> using namespace std; extern unsigned short thatNum; //声明thatNum是来自外部的 static bool printMe = false; int main() { unsigned short thisNum = 10; cout << thisNum << "i = " << getFac(thisNum) << endl; cout << thatNum << "i = " << getFac(thatNum) << endl; cout << headerNum << "i = " << getFac(headerNum) << endl; if(printMe) { cout << "that.cpp -> printMe = true 发挥作用" << endl; } else { cout << "this.cpp -> static boolean printMe 发挥作用" << endl; } return 0; }
that.cpp
#include "header.h" unsigned short thatNum = 8; bool printMe = true; unsigned long getFac(unsigned short num) { unsigned long sum = 1; for (int i=1; i<=num; i++) { sum *= i; } if(printMe) { return sum; } else { return 0; } }