g++コンパイル複数cpp

2992 ワード

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 <

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