C언어는 컴파일러를 필요로 한다. 아무리 멋진 프로그램을 만들어라도 컴파일을 해야 작동한다. C언어를 컴파일 해줄 수 있는 컴파일러는 여러가지가 있는데, 이에 따라 C언어의 종류를 나눌 수 있다. 컴파일러를 만드는 회사는 여러곳이 있는데, 볼랜드의 Turbo C, 마이크로소프트의 MS C, 인텔의 Intel C complier, 그리고 GPL에 따라 자유롭게 쓸 수 있는 GNU C가 있다. 컴파일러의 실행화일 이름은 C complier를 따서 cc로 되어 있고, 각각 앞에 자기네 회사 첫 글자를 따서 tcc, mcc, icc, gcc등등으로 부른다.

물론, 난 리눅스에서 기본적으로 자유롭게 사용할 수 있는 gcc를 기준으로 설명할 것이다. 다른 컴파일러들 사용하는 것도 그다지 어렵지는 않다. 컴파일러의 설치는 알아서 다들 해 보시기 바란다.

컴파일하는 방법은 아주 쉽다. 예를들어, example.c라는 프로그램을 만들었다고 하자. example.c는 메모장에서 열어보면 그냥 생으로 보이는, 아직 요리가 되지 않은 날것이다. 이것을 컴퓨터의 메모리 속에 쑤셔넣기 위해서는 바이너리로 바꿔줘야 하는데, 그렇게 하려면 다음과 같이 명령하면 된다.
gcc example.c
그럼 a.out이라는 파일이 거기에 새로 생성될 것이다. 이제 a.out을 실행시키면 된다. 실행 방법은, 리눅스의 경우에는 ./a.out 이라고 해야 한다. 윈도우는 뭐 그냥 두번 클릭하면 되겠다. 하지만 맨날 a.out만 만들 수는 없잖은가. 그럼 목적지를 얘기해 주면 된다.
gcc example.c -o example
이렇게 하면 -o 뒤에 있는 파일 이름으로 컴파일 되어 출력된다.
다른건 그다지 어려운게 없는데, 만약 수학 함수 헤더인 math.h를 include해서 컴파일 할 경우에는 에러가 뜰 것이다. 이 경우, link math 옵션을 붙여야 한다. 이것때문에 괜히 삽질하는 경우가 많다.
gcc example.c -o example -lm
그럼 에러가 안나올 것이다.

C언어 프로그램을 컴파일 할 때 나오는 에러는 전부 의미가 있는 내용으로, 부디 에러가 아무것도 없는 프로그램을 만들 수 있기를 바란다.

아래는 gcc --help 명령을 쳤을 때 나오는 내용이다.
Usage: gcc [options] file...
Options:
  -pass-exit-codes         Exit with highest error code from a phase
  --help                   Display this information
  --target-help            Display target specific command line options
  (Use '-v --help' to display command line options of sub-processes)
  -dumpspecs               Display all of the built in spec strings
  -dumpversion             Display the version of the compiler
  -dumpmachine             Display the compiler's target processor
  -print-search-dirs       Display the directories in the compiler's search path
  -print-libgcc-file-name  Display the name of the compiler's companion library
  -print-file-name=<lib>   Display the full path to library <lib>
  -print-prog-name=<prog>  Display the full path to compiler component <prog>
  -print-multi-directory   Display the root directory for versions of libgcc
  -print-multi-lib         Display the mapping between command line options and
                           multiple library search directories
  -print-multi-os-directory Display the relative path to OS libraries
  -Wa,<options>            Pass comma-separated <options> on to the assembler
  -Wp,<options>            Pass comma-separated <options> on to the preprocessor
  -Wl,<options>            Pass comma-separated <options> on to the linker
  -Xassembler <arg>        Pass <arg> on to the assembler
  -Xpreprocessor <arg>     Pass <arg> on to the preprocessor
  -Xlinker <arg>           Pass <arg> on to the linker
  -combine                 Pass multiple source files to compiler at once
  -save-temps              Do not delete intermediate files
  -pipe                    Use pipes rather than intermediate files
  -time                    Time the execution of each subprocess
  -specs=<file>            Override built-in specs with the contents of <file>
  -std=<standard>          Assume that the input sources are for <standard>
  --sysroot=<directory>    Use <directory> as the root directory for headers
                           for headers and libraries
  -B <directory>           Add <directory> to the compiler's search paths
  -b <machine>             Run gcc for target <machine>, if installed
  -V <version>             Run gcc version number <version>, if installed
  -v                       Display the programs invoked by the compiler
  -###                     Like -v but options quoted and commands not executed
  -E                       Preprocess only; do not compile, assemble or link
  -S                       Compile only; do not assemble or link
  -c                       Compile and assemble, but do not link
  -o <file>                Place the output into <file>
  -x <language>            Specify the language of the following input files
                           Permissible languages include: c c++ assembler none
                           'none' means revert to the default behavior of
                           guessing the language based on the file's extension
 
Options starting with -g, -f, -m, -O, -W, or --param are automatically
 passed on to the various sub-processes invoked by gcc.  In order to pass
 other options on to these processes the -W<letter> options must be used.
이게 정확히 뭔얘기인지는 나도 잘 모르겠다. 다만, 프로그램을 복잡하게 만들수록 위의 옵션들을 사용하게 될 일이 있을테니, 만약 프로그램이 완벽한데 컴파일이 이상하게 된다면 옵션을 잘 살펴볼 일이다.




by snowall 2006. 12. 31. 23:12