g++编译多个cpp

复习一下g++编译多文件


auto.h

extern const int eInt;

int getConstValue();

auto.cpp文件

#include "iostream"
#include "typeinfo"
#include "math.h"
#include "auto.h"







const int eInt = 920;


int iInit = 79;
int getSize(){
	return iInit;
}

const int mSize = getSize();

int main(){

	char h = 'e';
	int a = 10;
	auto b = a;
	auto c = std::string("you are right");
	std::string f = "hel";
	std::cout << typeid(b).name() << std::endl;
	std::cout << typeid(a).name() << std::endl;
	std::cout << typeid(h).name() << std::endl;
	std::cout << typeid(f).name() << std::endl;

	std::cout << "c = " << c << std::endl;	
	std::cout << typeid(c).name() << std::endl;

	//	std::string str;
	//	while ( std::cin >> str){
	//		std::cout << "cin = " << str << std::endl;
	//	}

	int i = 92, &i2 = i;
	std::cout << "i2 = " << i2 << std::endl;

	int sum = 0;
	for (int i = 0; i != 11; i++){
		sum += i;
	}

	std::cout << "sum = " << sum << std::endl;


	auto sum1 = 0;
	for (int i = 1; i <= 10; i++){
		sum1 += pow(2,i-1);
	}


	auto sum2 = 0;
	for (int m = 1; m <= 10; m++){

		auto an = 1;
		for (int n = 1; n < m; n++){
			an = an*2;
		} 

		sum2 += an;
	}

	std::cout << "sum1 = " << sum1 << std::endl;
	std::cout << "sum2 = " << sum2 << std::endl;


	//等差数列  
	//an = a1+ (n-1)*p;
	//sn = n*(a1+an)/2;
	//等比数列
	//an = a1*pow(q,n-1)
	//sn = a1*(1-pow(q,n) )/(1-q)

	int *p = nullptr;

	int ic1 = 10, ic2 = 7;
	int *p1 = &ic1, *p2 = &ic2;


	int iend  = *p1 * *p2;
	std::cout << "iend = " << iend << std::endl;

	iInit = 28;
	std::cout << "mSize = " << mSize << std::endl;

	const int ii = getSize();
	std::cout << "ii = " << ii << std::endl;

	bool bValue = false;
	bool bv = true;
	bool bv1 = 83;
	std::cout << "bValue = " << bValue << std::endl;
	std::cout << "bv = " << bv << std::endl;
	std::cout << "bv1 = " << bv1 << std::endl;

	std::cout <<" getConstValue() = " << getConstValue() << std::endl;	
	return 0;	
}


use.cpp

#include "auto.h"


int getConstValue(){
	return eInt;
}

使用g++编译

g++  -c auto.cpp use.cpp -std=c++11

g++ use.o auto.o -o a.out

执行g++ -c 编译生成中间文件 auto.o与use.o.

执行g++  -o 将中间文件链接成 a.out文件.

执行   ./a.out得到结果

  

andrew@andrew-pc /mnt/e/work/studycpp/auto $ ./a.out 
i
i
c
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
c = you are right
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
i2 = 92
sum = 55
sum1 = 1023
sum2 = 1023
iend = 70
mSize = 79
ii = 28
bValue = 0
bv = 1
bv1 = 1
 getConstValue() = 920




你可能感兴趣的:(c++)