2007年3月10日星期六

一个Makefile的例子

# 配置部分
# 下面的变量可以在shell 的环境变量里面指定。
# 也可以象下面这样在 Makefile 里面指定。
# CC=gcc # 编译器
# CFLAGS=-Wall -Werror -g # 编译器参数
# LD=gcc # 连接器参数
# LDFLAGS= $(
LIBS
) -lpthread # 连接器参数
# DEPENDFLAG=-MM # 生成依赖关系文件的参数
# INCLUDES=-Idir1 -Idir2 # 指明包含外部头文件的目录
# LIBS=-la -lb -lc # 指明引用外部的苦文件
CFLAGS:=$(CFLAGS) $(INCLUDES)
LDFLAGS:=$(LDFLAGS) $(LIBS)

#指明项目中,包含源程序的所有的子目录。
SRCDIRS=.
#指明最终生成的可执行文件的名称
PROGRAMS=test.exe

#下面的部分一般不用改动

#从所有子目录中得到源代码的列表
SRCS=$(foreach dir,$(SRCDIRS),$(wildcard $(dir)/*.c))

#得到源代码对应的目标文件的列表
OBJS=$(SRCS:.c=.o)

#得到源代码对应的依赖关系文件的列表
#依赖关系文件就是一个目标文件依赖于
#哪些头文件和源程序,依赖关系是自动
#生成的,并且用 include 语句包含在
#Makefile 中。
DEPENDS=$(SRCS:.c=.d)

#指明默认目标是生成最终可执行文件。
all: $(PROGRAM)

#生成依赖关系文件
%.d:%.c
$(CC) $(DEPENDFLAG) $(CFLAGS) $< |\
sed "s?\\(.*\\):?$(basename $<).o $(basename $<).d :?g" \
> $@ || $(RM) $@

$(PROGRAMS): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $(filter %.o ,$+)

# 包含入依赖关系文件
include $(DEPENDS)

# 删除生成的中间文件
clean:
rm $(OBJS) $(DEPENDS) $(PROGRAMS)

2007年3月8日星期四

linux下的getch函数与kbhit函数

// 在linux下需要修改terminal的属性

#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>

#include <stdlib.h>

#include <sys/ioctl.h>

#include <sys/time.h>

#include <sys/types.h>

 

#undef TERMIOSECHO

#define TERMIOSFLUSH

 

/*
 * kbhit() -- a keyboard lookahead monitor
 *
 * returns the number of characters available to read
 */

static int kbhit ( void )
{
    struct timeval tv;
    struct termios old_termios, new_termios;
    int            error;
    int            count = 0;

    tcgetattr( 0, &old_termios );
    new_termios              = old_termios;
    /*
     * raw mode
     */

    new_termios.c_lflag     &= ~ICANON;
    /*
     * disable echoing the char as it is typed
     */

    new_termios.c_lflag     &= ~ECHO;
    /*
     * minimum chars to wait for
     */

    new_termios.c_cc[VMIN]   = 1;
    /*
     * minimum wait time, 1 * 0.10s
     */

    new_termios.c_cc[VTIME]  = 1;
    error                    = tcsetattr( 0, TCSANOW, &new_termios );
    tv.tv_sec                = 0;
    tv.tv_usec               = 100;
    /*
     * insert a minimal delay
     */
    select( 1, NULL, NULL, NULL, &tv );
    error                   += ioctl( 0, FIONREAD, &count );
    error                   += tcsetattr( 0, TCSANOW, &old_termios );
    return( error == 0 ? count : -1 );
/* end of kbhit */
/*------------------------------------------------*/
int getch( void )

{
      int c = 0;

      struct termios org_opts, new_opts;
      int res = 0;

          //-----  store old settings -----------
      res = tcgetattr( STDIN_FILENO, &org_opts );
      assert( res == 0 );

          //---- set new terminal parms --------
      memcpy( &new_opts, &org_opts, sizeof(new_opts) );
      new_opts.c_lflag &= ~( ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL );
      tcsetattr( STDIN_FILENO, TCSANOW, &new_opts );
      c = getchar();

          //------  restore old settings ---------
      res = tcsetattr( STDIN_FILENO, TCSANOW, &org_opts );
      assert( res == 0 );
      return( c );
}