cpp

2022/03/15

Linking

Build

c++ source file -(compile)-> object file

Preprocess

replace the line containing the `#include` directive with the entire content of the included file.

Compile

Shared Library

자주 사용하는 외부 함수를 실행 프로그램에 포함하는 경우 덩치가 커지고 업그레이드 시 이를 다시 컴파일해야 함 공유 라이브러리 형식으로 만들어놓고, 컴파일 시점에 사용할 라이브러리를 연결만 하는 방법을 사용

공유 라이브러리와 연결된 프로그램 실행 시 내부적으로 dyanmic loader 프로그램이 먼저 동작하며,

loader는 Shared library를 찾을 때 LD_LIBRARY_PATH 같은 환경변수를 참고

`ldd IfcConvert` 실행 시 프로그램이 참고하는 shared library를 확인할 수 있다

dynamic loading

ldconfig -> updates the links/cache which the dynamic loader uses

readelf -a IfcConvert | grep NEEDED

Make

make is a powerful tool that automates most of the build process

target: prerequisite1 prerequisite2 ...
command1
command2 ...

target: the name of a command-generated file prerequisite: this file is used to create the target. a target often requires multiple files command: an action that make carries output

generalizing makefiles

SOURCES = hello.c helper.c
OBJS = $(SOURCES:.c=.o)
TARGET = hello

CC =gcc  # compiler
OPTS = -Wall # flags
LIBS = -lm # libraries

$(TARGET): $(OBJS)
  $(CC) -o $(TARGET) $(OBJS) $(LIBS)

%.o: %.c
  $(CC) $(OPTS) -c $< -o $@

clean:
  rm -f $(OBJS) $(TARGET)

Debugging

gdb

compiling with the -g flag gcc -g -o buggy buggy.c

opening gdb with the executable file gdb buggy

디버거 세션이 실행된다.


이 문서와 연결된