Pertanyaan :Saya ingin memahami dasar-dasar cara menulis, mengkompilasi, dan menjalankan program C di Linux OS. Bisakah Anda menjelaskannya dengan contoh sederhana?
Jawaban : Dalam artikel ini, mari kita tinjau dengan cepat cara menulis program C Hello World basic dasar dan cara mengkompilasi program *.c di Linux atau Unix OS.
1. Tulis Program Hello World C
Buat program helloworld.c menggunakan editor Vim seperti yang ditunjukkan di bawah ini.
$ vim helloworld.c /* Hello World C Program */ #include<stdio.h> main() { printf("Hello World!"); }
2. Pastikan C Compiler (gcc) terinstal di sistem Anda
Pastikan gcc diinstal pada sistem Anda seperti yang ditunjukkan di bawah ini.
$ whereis cc cc: /usr/bin/cc /usr/share/man/man1/cc.1.gz $ which cc /usr/bin/cc $ dpkg -l | grep gcc ii gcc 4:4.3.3-1ubuntu1 The GNU C compiler ii gcc-4.3 4.3.3-5ubuntu4 The GNU C compiler ii gcc-4.3-base 4.3.3-5ubuntu4 The GNU Compiler Collection (base package) ii gcc-4.3-doc 4.3.3-5ubuntu4 Documentation for the GNU compilers (gcc, go ii gcc-4.3-locales 4.3.3-5ubuntu4 The GNU C compiler (native language support ii gcc-4.3-multilib 4.3.3-5ubuntu4 The GNU C compiler (multilib files) ii lib64gcc1 1:4.3.3-5ubuntu4 GCC support library (64bit) ii libgcc1 1:4.3.3-5ubuntu4 GCC support library
3. Kompilasi Program helloworld.c
Kompilasi helloworld.c menggunakan perintah cc seperti gambar di bawah ini. Ini akan membuat file a.out.
$ cc helloworld.c $ ls -l -rw-r--r-- 1 ramesh ramesh 71 2009-08-28 14:06 helloworld.c -rwxr-xr-x 1 ramesh ramesh 9152 2009-08-28 14:07 a.out
4. Jalankan Program C (a.out)
Anda dapat menjalankan a.out untuk melihat output (atau) mengganti namanya menjadi nama lain yang bermakna dan menjalankannya seperti yang ditunjukkan di bawah ini.
$ ./a.out Hello World! $ mv a.out helloworld $ ./helloworld Hello World!