Domanda :Vorrei capire le basi di come scrivere, compilare ed eseguire un programma C su Linux Sistema operativo. Puoi spiegarlo con un semplice esempio?
Rispondi : In questo articolo, esaminiamo molto rapidamente come scrivere un programma Hello World C di base e come compilare il programma *.c su sistema operativo Linux o Unix.
1. Scrivi un programma Hello World C
Crea il programma helloworld.c usando un editor Vim come mostrato di seguito.
$ vim helloworld.c /* Hello World C Program */ #include<stdio.h> main() { printf("Hello World!"); }
2. Assicurati che C Compiler (gcc) sia installato sul tuo sistema
Assicurati che gcc sia installato sul tuo sistema come mostrato di seguito.
$ 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. Compila il programma helloworld.c
Compila helloworld.c usando il comando cc come mostrato di seguito. Questo creerà il 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. Eseguire il programma C (a.out)
Puoi eseguire a.out per vedere l'output (o) rinominarlo con un altro nome significativo ed eseguirlo come mostrato di seguito.
$ ./a.out Hello World! $ mv a.out helloworld $ ./helloworld Hello World!