Skip to content

Instantly share code, notes, and snippets.

@ananyo2012
Last active July 6, 2019 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ananyo2012/b295756eb0614ed29802aed3f8c407dd to your computer and use it in GitHub Desktop.
Save ananyo2012/b295756eb0614ed29802aed3f8c407dd to your computer and use it in GitHub Desktop.
gcc commands

gcc -o hello hello.c - Create executable with name hello

gcc -c hello.c - Preprocess and compile only

gcc hello.o -lm - tells the compiler(ie. gcc) to link the executable file with the given library (math.h here)

gcc -x c hello.txt - -x option asks for source language

gcc -std=89 hello.c - -std option is for specifying standard

gcc -I ./sources -o hello hello.c - -I option is for path to header files

Creating and using Static libraries

gcc -c greet.c - Create greet.o after compiling greet.c

ar cr libgreet.a greet.o - Create static library (archive) from object file

ar -tvf libgreet.o - See the contents of an archive file

gcc -L ./ -o hello hello.c -lm -lgreet - -L option specifies the directory to search the static library

Creating and using Dynamic libraries

gcc -c -fPIC greet.c - Generate object file in fixed position code format

gcc hello.o -shared -o libgreet.so - Create dynamic library (.so) from object file

gcc -I ./headers -L ./ -o hello hello.c -lgreet - Link dynamic library during linking stage

Other useful flags

-Wall - enable all the compile time warning messages

-O<level> - This option is used to specify the code optimisation level. <level> here is an integer. This way while generating binary code for macros, or loop statements, or pointer/string operations, gcc can produce more efficient/faster code.

-g - Adds extra information to the executable which can be used for debugging

-S - Used to produce the compilation output in an assembly language, i.e. a step just before the code generation. It produces a .s file while contains program in assembly code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment