https://www.ostechnix.com/compile-run-c-c-programs-linux
This brief tutorial will explain how to compile and run
C/C++ programs in GNU/Linux. If you’re a student or a new Linux user
coming from Microsoft platform, then you might be wondering how to run
the C or C++ programs in a Linux distribution. Because, compiling and
running code in Linux platforms is little bit different than Windows.
Let us get started, shall we?
The development tools includes all necessary applications, such as GNU GCC C/C++ compilers, make, debuggers, man pages and others which are needed to compile and build new software, packages etc.
Also, there is a script named manji that helps you to setup a complete environment in Ubuntu-based systems.
After installing the necessary development tools, verify them using any one of the following commands:
Here is a simple “C” program.
Finally, execute the program using command:
To compile multiple source files (Eg. source1 and source2) into executable, run:
If your program contains math functions:
Cheers!
Setup Development Environment
As you may already know, to run the code we need to install the necessary tools and compilers, right? Yes! Refer the following guide to install all development tools in your Linux box.The development tools includes all necessary applications, such as GNU GCC C/C++ compilers, make, debuggers, man pages and others which are needed to compile and build new software, packages etc.
Also, there is a script named manji that helps you to setup a complete environment in Ubuntu-based systems.
After installing the necessary development tools, verify them using any one of the following commands:
whereis gcc
which gcc
gcc -vThese commands will display the installation path and version of gcc compiler.
Compile And Run C/C++ Programs In Linux
Write your code/program in your favorite CLI/GUI editor. Use extension .c for C programs or .cpp for C++ programs.Here is a simple “C” program.
nano ostechnix.c
#includeTo compile the program, run:int main() { printf("Welcome To OSTechNix!"); return 0; }
gcc ostechnix.c -o ostechnix1Or,
g++ ostechnix.c -o ostechnix1In the above example, we used C++ compiler to compile the program. To use C compiler instead, use:
cc ostechnix.c -o ostechnix1If there is any syntax or semantic errors in your code/program, it will display them. You might need to fix them first to proceed further. If there is no error then the compiler will successfully generate an executable file named ostechnix1 in the current working directory.
./ostechnix1
To compile multiple source files (Eg. source1 and source2) into executable, run:
gcc source1.c source2.c -o executableTo allow warnings, debug symbols in the output:
gcc source.c -Wall -Og -o executableTo compile the source code into Assembler instructions:
gcc -S source.cTo compile the source code without linking:
gcc -c source.cThe above command will create a executable called source.o.
If your program contains math functions:
gcc source.c -o executable -lmFor more details, refer the man pages.
man gccAnd, that’s all for now. If you find out guides useful, recommend them to your colleagues and friends and support OSTechNix.
Cheers!
No comments:
Post a Comment