Rylah's Study & Daily Life

Modern C++ : 03. C++ Build - C++ 빌드 컴파일 Intro, 빌드 프로세스 본문

Study/C++

Modern C++ : 03. C++ Build - C++ 빌드 컴파일 Intro, 빌드 프로세스

Rylah 2022. 2. 3. 14:19

Build Process

 - 다른 언어와는 다르게 복잡한 편이다.

 

main.cpp

cat.h

cat.cpp

 

파일들이 있다고 가정하자

 

빌드에서 가장 먼저 개입 되는 것은

pre-processor - #include, #define을 치환해주는 역할

 

cat.h <-> main.cpp  => translation unit

cat.h <-> main.cpp => Translation unit

 

이 Translation unit을 Compiler가 obj 파일로 변환

 

code
data

obj

 

이러한 obj 파일들을 링커가 개입해서 필요한 obj 파일을 모아서 실행 파일로 

code
data
excute한 여러 정보들

프로세스가 만들어지게 된다.

 

이러한 프로세스를 진행 해주는 것을 컴파일러 라고 한다.

 

컴파일러의 종류

https://en.wikipedia.org/wiki/List_of_compilers

 

List of compilers - Wikipedia

Wikimedia list article This page is intended to list all current compilers, compiler generators, interpreters, translators, tool foundations, assemblers, automatable command line interfaces (shells), etc. Ada Compilers[edit] ALGOL 60 compilers[edit] ALGOL

en.wikipedia.org

<gcc 컴파일러 다양한 옵션 키워드>

Command Line 동작
g++ ~~.cpp -o Name Name이라는 파일명을 생성해서 실행 가능
~~ -Wall warning을 컴파일중에 경고해줍니다. warning은 더 안전한 코드를 만드는데 도움을 줌
-Wall - Werror warning -> error을 변환
std=c++17 C++17  컴파일 가능하게
-g 디버그 
-v 상세한 컴파일 과정을 알 수 있음
-O0 or -O2 Optimization 레벨을 0 , 2로 설정 그 외에도 다양한 옵션은 문서를 참고 할 수 있음
-march 'cputype' 해당하는 Architectrue에 맞는 코드를 생성할 수 있음 , native는 현재 컴퓨터

 

 

#include <iostream>

int main(void)
{
	std::cout << "hi" << std::endl;
	return 0;
}

#include <iostream>

int main(void)
{
	int i; // not initialized
	//std::cout << "hi" << std::endl;
    std::cout << "hi" << i << std::endl;
	return 0;
}

Warning을 Error로 표기하는 -Werror을 하게 되면 변수가 사용되지 않은 warning도 error로 처리해서 좀 더 안전한 코드 작성을 유도 할 수 있다.

#include <iostream>
#include <variant> // C++17

int main(void)
{
	std::variant<int, float> i = 0;
	//int i; // not initialized
	//std::cout << "hi" << std::endl;
	//std::cout << "hi" << i << std::endl;
	std::cout << "hi" << std::endl;
	return 0;
}

std::variant는 C++17부터 사용가능한 문법이므로 컴파일러가 그냥 인식하지는 못한다.

-g 옵션은 디버그 정보를 같이 넣고 싶을 때 사용한다.

-v 컴파일 과정을 자세히 보고싶으면 -v를 넣으면 된다.

Optimization Level을 정하는 옵션이다.

https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

 

Optimize Options (Using the GNU Compiler Collection (GCC))

Disable the optimization pass that scans for opportunities to use “decrement and branch” instructions on a count register instead of instruction sequences that decrement a register, compare it against zero, and then branch based upon the result. This o

gcc.gnu.org

자세한 옵션 정보는 이 쪽에서 참고 가능하다.

 

최적화 이야기 안에 -march 옵션은 Machine Architecture이다. 

https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html

 

x86 Options (Using the GNU Compiler Collection (GCC))

These switches enable the use of instructions in the MMX, SSE, SSE2, SSE3, SSSE3, SSE4, SSE4A, SSE4.1, SSE4.2, AVX, AVX2, AVX512F, AVX512PF, AVX512ER, AVX512CD, AVX512VL, AVX512BW, AVX512DQ, AVX512IFMA, AVX512VBMI, SHA, AES, PCLMUL, CLFLUSHOPT, CLWB, FSGSB

gcc.gnu.org

여러 옵션을 줄 수 있지만, 가장 중요한 것은 어디서든 잘 돌아갈 수 있게 코드를 잘 작성하는 것이다.

 

native는 현재 PC에 맞게, x86-64는 64비트 등이 될 수 있다.