第3节 A Hello World
推荐给好友
打印
加入收藏
更新于2008-05-15 18:46:01

The smallest possible program in C
main() {
}

Every C program must have the “main” function somewhere.

C is case sensitive.

Little more than smallest

Test0.c
int main()
{
 return(123);
}
Compile and link
$gcc –o test0 test0.c
$./test0; echo $?
123

Hello World!
#include <stdio.h>

int main(void) {
  printf(“Hello world!\n”);
}


Hello World, again…

#include <stdio.h>

void print_my_string(char str[]);

char hello[] = “Hello world!”;

int main(void) {
  print_my_string(hello);
  return(0);
}
void print_my_string(char str[]) {
  printf(“%s\n”, str);
}


  C Program Structure

    preprocessor commands
    type definitions
    function prototypes
    global variables
    functions

Basics on C compiling
Compile and run program file.c
> gcc file.c –o file –Wall –g
./file

Compile multiple sources
> gcc file1.c –c –Wall –g
> gcc file2.c –c –Wall -g
> gcc file1.o file2.o –o file
> ./file


Use Makefile to compile
Makefile
All:file

file1.o:file1.c
    gcc file1.c -c
file2.o:file2.c
    gcc file2.c -c
file:file1.o file2.o
    gcc file1.o file2.o -o file

Clean:
    rm -f *.o; rm -f file
Make, make clean


Use GDB/Insight to debug

Makefile
All:file

file1.o:file1.c
    gcc file1.c –c -g
file2.o:file2.c
    gcc file2.c –c -g
file:file1.o file2.o
    gcc file1.o file2.o -o file

Clean:
    rm -f *.o; rm -f file; rm -f core

Gdb file
Insight file

Basics on C compiling
> gcc file1.c –c –Wall –g
> gcc file2.c –c –Wall -g
> gcc file1.o file2.o –o file
> ./file


Makefile
All:file

file1.o:file1.c
file2.o:file2.c
file:file1.o file2.o

Clean:
    rm -f *.o; rm -f file

 

<<上一节             下一节>> 




 
关于我们 | 诚邀加盟 | 客户服务 | 相关法律 | 网站地图 | 友情链接 | 服务信箱:service@eefocus.com
© 2006 与非门科技信息咨询(北京)有限公司 All Rights Reserved.