Xin chào ! Nếu đây là lần đầu tiên bạn đến với diễn đàn, xin vui lòng danh ra một phút bấm vào đây để đăng kí và tham gia thảo luận cùng VnPro.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Viết chương trình "Hello world"!

    Chào mọi người, mình đang học viết chương trình "Hello world".

    Mong được học hỏi từ các bạn SV CNTT và các anh chị có kinh nghiệm phát triển các ứng dụng embedded applications trên nền open source.

    Ví dụ 1: Sử dụng Static Lib

    $ ls -l
    total 20
    -rw-r--r-- 1 gcs06 gcs 52 Apr 20 2004 hello1.c
    -rw-r--r-- 1 gcs06 gcs 52 Apr 20 07:41 hello2.c
    -rw-r--r-- 1 gcs06 gcs 53 Apr 20 07:41 hello3.c
    -rw-r--r-- 1 gcs06 gcs 172 Apr 20 07:43 main.c
    -rw-r--r-- 1 gcs06 gcs 388 Apr 20 2004 makefile

    $ cat hello1.c
    #include<stdio.h>
    hello1()
    { printf("Hello1\n");
    }

    $ cat hello2.c
    #include<stdio.h>
    hello2()
    { printf("Hello2\n");
    }

    $ cat hello3.c
    #include<stdio.h>
    hello3()
    { printf("Hello3\n");
    }

    $ cat main.c
    #include<stdio.h>

    extern void hello1();
    extern void hello2();
    extern void hello3();

    int main()
    { printf("Inside Main\n");
    hello1();
    hello2();
    hello3();

    return 0;
    }

    $ cat makefile
    LIB_OBJS= hello1.o hello2.o hello3.o

    LIB_FILE= libutil.a

    PROG = hello_world

    PROG_OBJS = main.o

    all: $(LIB_FILE) $(PROG)

    $(PROG): $(PROG_OBJS)
    gcc $(PROG_OBJS) -L. -lutil -o $(PROG)

    $(LIB_FILE): $(LIB_OBJS)
    ar rc $(LIB_FILE) $(LIB_OBJS)
    ranlib $(LIB_FILE)

    %.o: %.c

    gcc -c $<

    clean:
    rm -f $(PROG_OBJS) $(PROG) $(LIB_OBJS) $(LIB_FILE)

    cleanprog:
    rm -f $(PROG) $(PROG_OBJS)

    cleanlib:
    rm -f $(LIB_OBJS) $(LIB_FILE)

    $ make
    gcc -c hello1.c
    gcc -c hello2.c
    gcc -c hello3.c
    ar rc libutil.a hello1.o hello2.o hello3.o
    ranlib libutil.a
    gcc -c main.c
    gcc main.o -L. -lutil -o hello_world

    $ ls -l
    total 56
    -rw-r--r-- 1 gcs06 gcs 52 Apr 20 08:00 hello1.c
    -rw-r--r-- 1 gcs06 gcs 908 Apr 20 2004 hello1.o
    -rw-r--r-- 1 gcs06 gcs 52 Apr 20 07:41 hello2.c
    -rw-r--r-- 1 gcs06 gcs 908 Apr 20 2004 hello2.o
    -rw-r--r-- 1 gcs06 gcs 53 Apr 20 07:41 hello3.c
    -rw-r--r-- 1 gcs06 gcs 908 Apr 20 2004 hello3.o
    -rwxr-xr-x 1 gcs06 gcs 14179 Apr 20 2004 hello_world
    -rw-r--r-- 1 gcs06 gcs 3010 Apr 20 2004 libutil.a
    -rw-r--r-- 1 gcs06 gcs 172 Apr 20 07:43 main.c
    -rw-r--r-- 1 gcs06 gcs 1020 Apr 20 2004 main.o
    -rw-r--r-- 1 gcs06 gcs 388 Apr 20 08:00 makefile

    $ ./hello_world
    Inside Main
    Hello1
    Hello2
    Hello3

    $ make clean
    rm -f main.o hello_world hello1.o hello2.o hello3.o libutil.a

    $ ls -l
    total 20
    -rw-r--r-- 1 gcs06 gcs 52 Apr 20 08:00 hello1.c
    -rw-r--r-- 1 gcs06 gcs 52 Apr 20 07:41 hello2.c
    -rw-r--r-- 1 gcs06 gcs 53 Apr 20 07:41 hello3.c
    -rw-r--r-- 1 gcs06 gcs 172 Apr 20 07:43 main.c
    -rw-r--r-- 1 gcs06 gcs 388 Apr 20 08:00 makefile

    Cheers,

  • #2
    Bạn nên thảm khảo tại địa chỉ nay nhé có một bài viết rất chi tiết, nó sẽ hướng dẫn bạn từ đầu đến cuối để viết một chương trình C trên Linux hoặc Unix

    Hy vọng sẽ giúp ích cho bạn

    Comment

    Working...
    X