
gcc : cross-compile :xxx.c라는 source file이 있다고 가정.

preprocessing:cpp(C Preprocess)라는 명령어를 수행하여 .c(source code)에 있는 macro들(#inclue, #define)을 처리하여.i(preprocessed code)로 만들어 준다.gcc -E 옵션

compiling:gcc -S는 .c(source code) 또는 .i(preprocessed code)을 .s(assembly code)로 만들어준다.
assembling:as라고 하는 assembler가 .s(assembly code)를 .o(object file)로 만들어 준다.gcc -c



linking:ld라고 하는 linker가 object file(*.o)와 library들을 linking하여 실행파일(a.out)로 만들어줌
반면, 아래와 같이 gcc 명령어는 내부적으로 ld를 호출하면서 필요한 모든 파일을 자동으로 linking해줌. (-v 옵션은 gcc가 내부적으로 어떻게 동작하는지 확인하기 위함)

Front End :Middle End :Back End :(정리) 그래서 왜 gcc SW는 세 가지 components로 나눠져 있나?
새로운 언어 또는 새로운 CPU(platform)가 개발되었다면?
FrontEnd 또는 BackEnd에서 각 상황에 맞는 부분만 개발하면 되기 때문
(새로운 코드 -> AST 형태), (RTL -> (machine code) -> 새로운 platform)

-o : tells the compiler to name the executable-g : adds symbolic information to hello for debugging-Wall : tells it to print out all warnings-E: generate preprocessed code-S: generate assembly code-c: generate object code (compile without linking)-o <filename>: specify output filename -g: include debugging symbols-Wall: show all warnings-ansi, pedantic: be stubborn aboutn standards-O, -O0, -O1, -O2, -Os...: Optimizations-c : Stop after creating object files, don't link
(linking단계 직전인 assembling까지만 하라.)
= (실행파일을 만들지 말고 object file까지만 만들어라)

-o file명 : 지정한 file명으로 실행파일을 생성하라.

-O2 -Wall -pedantic : example 1 :
main 함수에 foo()함수와 goo()함수 호출 후 종료


example 2: moduling programming & linking each object files




각각의 source file들을 object files로 compile (linking 직전까지)
이때, ./include 디렉토리에 funcs.h라는 header file을 선언했기 때문에, gcc -Idir 옵션으로 header file 탐색 경로를 지정해줘야 함.
그렇지 않으면 아래와 같이 header file을 못 찾음.
이제 제대로 gcc -Idir 을 사용해서 각 source file들을 object file로 compile (linking 직전까지)

example 3 : example 2 + 'header guard' in funcs.h





mac에서는 default compiler를 gcc 말고, clang을 사용.
그래서 gcc --version 하면, 내부적으로 clang을 실행.
gcc는 clang의 alias.

아래는 clang의 manual page 일부.
gcc의 옵션과 유사함.





Library :
compile된 다양한 object file(.o file)들을 모아놓은 file들.
(libc라는 library 안에 printf, scanf, strcmp, ...)
(libthread라는 library 안에 pthreadcreate, pthreadjoing, ...)
Library는 두가지 type으로 나눌 수 있다.
Static Library (*.a) : Link at compile time

ar : creates, updates, lists and extracts files from the library

nm : 생성한 정적 라이브러리에 어떤 함수들이 있는지 확인할 수 있다.

example 1 : 'libmystatic' 이라는 static library 만들기 (이 library에는 foo.o, goo.o가 있음)








example 2 : nm(생성한 정적 라이브러리에 만든 함수들이 있는지 확인할 수 있다.)
example 3 : ldd (print shared object dependencies)


example 4: -static option. example 3와 똑같음.


Shared Library : Linked to any program at run-time
shared library 장점 1:shared library 장점 2:reentrant = No global variables :position-independent :libmyfuncs.so : libmyfuncs.so.1 : libmyfuncs.so.1.0 : 
LD_LIBRARY_PATH=$HOME/foo/libexample 1:










mac에서는 ldd 명령어가 없음.
대신, otool (object file displaying tool) 명령어 사용.


mac에서는 LD_LIBRARY_PATH라는 환경 변수가 아니라
DYLD_LIBRARY_PATH라는 환경 변수에 library path를 추가해야 함.

