Compiling and linking

From SmartCommunity

Jump to: navigation, search

Compiling of C/C++ source files into object files means translating C/C++ instructions into machine code. It’s usually done by gcc with “-c” flag.

gcc -c file.c -o file.o

The flag “-c” in this example means the name of the file that needs to be compiled. Flag “-o” means output file. Some other useful flags are

  • -g — generates additional code for debugging
  • -O — enables code optimization (optimization may be tuned by chosing some level)
  • -I/path/to/some/dir — adds dir into the list of directories that are checked for included files
  • -L/path/to/some/dir — adds dir to the list of directories to be checked for library object files
  • -llibrary_name — adds library_name to the list of libraries to be linked within the application
  • -Wall — enables all warnings
gcc -g -c file.c -o file.o -I/opt/somelib/include -Wall

The order of compiler flags does not matter.