본문으로 건너뛰기
개발 뉴스로
기타dev.to··원문 약 3

GCC 플래그 참고 사항

GCC Flags Notes

GNU C 컴파일러(GCC) 플래그에 대한 내 참고 사항 이는 GCC에게 소스 파일을 개체 파일(.o)로 컴파일하도록 지시하지만 실행 파일에 링크하지는 않습니다.

핵심 요약

자동 요약
  1. 1GNU C 컴파일러(GCC) 플래그에 대한 내 참고 사항 이는 GCC에게 소스 파일을 개체 파일(.o)로 컴파일하도록 지시하지만 실행 파일에 링크하지는 않습니다.
  2. 2gcc -c main.c 이렇게 하면 다음이 생성됩니다.
  3. 3main.o 그런 다음 별도로 링크할 수 있습니다.

원문 본문

출처 · dev.to

My notes on GNU C Compiler (GCC) Flags

Basic Flags

-c

It tells GCC to compile the source file into an object file (.o), but do not link it into an executable.

gcc -c main.c 

This produces:

main.o 

Then you can link it separately:

gcc main.o -o main 

-o

It specifies the output filename.

gcc main.c -o main 

We get:

main 

-l

It means "link this library".
For example:

gcc main.c -lm -o main 

Here:

  • -l -> link a library
  • m -> library name libm
  • -o main -> output executable named main

The library must be present in the standard search paths of GCC.

-I

It specifies an additional directory to search for header files.

gcc -Iinclude main.c -o main 

This tells GCC:
When you see #include "something.h", also look inside the include/ directory.

-L

It specifies an additional directory to search for libraries during linking.
For example:

gcc main.c -L./lib -lmylib -o main 

This means:

  • -L./lib -> search ./lib for libraries
  • -lmylib -> link libmylib
  • -o main -> produce main

-static

It tells the linker to create a statically linked executable.

gcc main.c -static -o main 

Optimisation

-O

Sets the level of optimisation.

gcc -O2 main.c -o main 

Optimisation Levels

  • 0 -> does nothing
  • 1 -> basic optimisation
  • 2 -> produces small binaries
  • 3 -> produces fast binaries (may not be small in size)
1 -> for fast compile times 2 -> for production 0 -> for debugging 

Strict Rules, Warnings and Errors

Basic ones are:

-std

It specifies which C standard the compiler should follow.

gcc -std=c17 main.c -o main 

-Wall

-Wextra

-Wpedantic

-Werror

Some extra ones:

-W

-Wconversion

-Wshadow

-Wcast-qual

-Wwrite-strings

Debugging Related

-g

It generates debugging information in the executable/object file.

gcc -g main.c -o main 

It mainly tells GCC to include information that debuggers such as GDB can use.

Preprocessing Related

-E

It says run only the preprocessor.

gcc -E main.c 

-D

It defines a macro from the commmand-line:

gcc -DTEST main.c 

Here:

  • -D -> define macro
  • TEST -> name of the macro

Thats it! 😵‍💫

For further actions, you may consider blocking this person and/or reporting abuse

이 글은 dev.to 의 원문을 정제해 보여드립니다. 저작권은 원저작자에게 있습니다.

#c#notes

전체 내용이 궁금하다면

dev.to 원문에서 이어 읽기

원문 보기

비슷한 글

5유사도 추천