cc command in Linux with Examples
‘cc’ command stands for C Compiler, usually an alias command to ‘gcc’ or ‘clang’. As the name suggests, executing the ‘cc’ command will usually call the ‘gcc’ on Linux systems. It is used to compile the C language codes and create executables. The number of options available for the cc command is very high. The most important options are explained below with examples.
Syntax
cc [options] files
where,
- [options]: Various compiler options that modify how the source code is compiled.
- files: The C source code files that need to be compiled.
Basic ‘cc’ Command Example
Below command will compile the ‘source_file.c’ file, and create the default executable output file, ‘a.out’.
cc example.c
Important Options use with cc Command
1. cc command with -o option:
This command will compile the source_file.c file, and create an executable output file with the specified name.
cc example.c -o examp_out
2. cc command with -Wall option:
This command will compile the ‘source_file.c’ file, and check for all errors and warnings in the program.
cc example.c -Wall -o examp_out
3. cc command with -w option:
This command will compile the ‘source_file.c’ file, but suppress all the warnings.
cc example.c -w
4. cc command with -g option:
This command will compile the ‘source_file.c’ file, and create a dgging version of the executable output file. This output file can be used by the debugger, and is usually much larger in size than the normal output files.
cc example.c -g -o examp_out_debugebu
5. cc command with -c option:
This command will compile the ‘source_file.c’ file, and create an object file ‘source_file.o’, which can later be linked to create an executable output file.
cc example.c -c
6. cc command with -Ldir option:
This command, while compiling the ‘source_file.c’ file, will also search the specified directory for header files.
cc example.c -L /home/mukkesh/Desktop
7. cc command with -ansi option:
This command will compile the ‘source_file.c’ file, and makes sure that the code follows strict ANSI standards. This will make sure that the code is portable to be compiled on any other system.
cc -ansi example.c
8. cc command with ‘-dump’ options:
These commands specified below will print the version of the cc command, the default target machine and built-in specification strings.
cc -dumpversion
cc -dumpmachine
cc -dumpspecs
9. cc command with -v option:
This command will compile the ‘source_file.c’ file, and gives a verbose output.
cc example.c -v
Conclusion
The cc command is a powerful and flexible tool for compiling C programs in Linux. cc can help you optimize your builds, catch errors early, and generate debugging information when needed. By leveraging options like -Wall for warnings, -g for debugging, and -c for generating object files, you can ensure that your C programs are built efficiently and correctly.
cc command in Linux – FAQs
What is the cc command used for?
The cc command is used to compile C source code into executables or object files. It is typically an alias for GCC or Clang and is the primary tool for building C programs on Linux systems.
How can I specify a different name for the output executable?
Use the -o option followed by the desired file name. For example:
cc example.c -o my_program
How can I enable warnings during compilation?
To enable all warnings, use the -Wall option:
cc example.c -Wall -o my_program
What does the -g option do?
The -g option compiles the source code with debugging symbols, allowing you to debug the program using a debugger like gdb.