1 minute read

1. basic compile

1-1 C workflow

1) compile

  source file(.c) + header file(.h) = object file(.o)

  1 source file => 1 object file

5) execute or debug

1-2 compile command

direct

gcc file.c

result: create a.out

create object file

gcc -c file.c

result: create file.o

gcc -o file file.o

result: create file(executable file)



2. library

2-1 static lib vs shared lib

static libshared lib
included in: compile runtime(when needed)
suffix .a .so

executable file size including static lib is bigger than shared lib.



3. macro

3-1 define macro

able to define undefined macro in source file with,

gcc -DVAR_NAME=VALUE file.c

3-2 conditional compile

make condition for check macro defined in source file with,

#ifdef MACRO
    ...
#endif



4. add header/lib

4-1 add standard lib

gcc file.c -lname

standard lib is libname.a in /usr/include/

4-2 add user header

gcc -I. file.c

include all header(.h) in current directory

gcc -L. file.c

include all lib(.a or .so) in current directory



5. create static lib

ar rcv lib_name.a objfile1.o objfile2.o ...

make lib_name.a lib with objfile1.o + objfile2.o + …

* only with obj file

* if command gcc -L. without including header,

ar t lib_name.a

print all obj file in lib_name.a

Categories:

Updated:

Leave a comment