Tuesday, August 12, 2008

How to compile the g++ object file with gcc

Here is an example shows how to compiler a program with gcc with objects files which are compiled by g++

Example codes:
( Reference from here )

test.c 
 #include
 #include
 #include "adio.h"
 #include "adstring.h"
 int main()
 {
  char input[21];
  char buffer[11];
  char buf[11];
  const char* str = "hello world!!";
  adstring_strcpy(buffer, str, 11);
  puts(buffer);
  adio_fgets(input, 21, stdin);
  puts(input);
  adstring_strcpy(buffer, input, 5);
  puts(buffer);
  int a = 12345;
  puts(adstring_itoa(buf, a, 11));
  puts(buf);
  return 0;
 }

adio.h
 #ifndef ADVENCE_IO_H
 #define ADVENCE_IO_H
 #include
 char* adio_fgets(char* buf, int num, FILE* fp);
 void adio_stdinclean(void);
 #endif

adstring.h
 #ifndef ADVENCE_STRING_H
 #define ADVENCE_STRING_H
 char* adstring_strcpy(char* to, const char* from, int num);
 char* adstring_itoa(char* to, int from, int num);
 #endif

adio.c
 #include
 #include
 char* adio_gets(char* buf, int num, FILE* fp)
 {
  char* find = 0;
  fgets(buf, num, stdin);
  if ((find = strrchr(buf, '\n')))
  {
   *find = '\0′;
  }
  else
  {
   while (fgetc(fp) != '\n');
  }
  return buf;
 }
 void adio_stdinclean()
 {
  while (getchar() != '\n');
 }

adstring.c
 #include
 #include
 char* adstring_strcpy(char* to, const char* from, int num)
 {
  int size = num-1;
  strncpy(to, from, size);
  if (strlen(from) >= size)
  {
   to[size] = '\0′;
  }
  return to;
 }
 char* adstring_itoa(char* to, int from, int num)
 {
  char tmp[11];
  sprintf(tmp, "%d", from);
  adstring_strcpy(to, tmp, num);
  return to;
 }



Make static library , here we assume object files need to be compiler in g++
 g++ adio.c adstring.c -Wall -c (this line will come out with adio.o adstring.o)
 ar rcs libadlib.a adio.o adstring.o (archieve into libadlib.a file)

 gcc test.c -I. -L. -ladlib -o test

Here, there are some errors say that the function with object files is not found.
The reason is that compiler will change c++'s function name while compiling.
In order to tell compiler not to change function name, we can use extern "C"

We can modify adio.c adstring.c into :

adio.c
 #include
 #include

 #ifdef __cplusplus
 extern "C" {
 #endif

 char* adio_gets(char* buf, int num, FILE* fp)
 {
  char* find = 0;
  fgets(buf, num, stdin);
  if ((find = strrchr(buf, '\n')))
  {
   *find = '\0′;
   }
  else
  {
   while (fgetc(fp) != '\n');
  }
  return buf;
 }
 void adio_stdinclean()
  {
  while (getchar() != '\n');
 }

 #ifdef __cplusplus
 }

 #endif

adstring.c
 #include
 #include

 #ifdef __cplusplus
 extern "C" {
 #endif


 char* adstring_strcpy(char* to, const char* from, int num)
 {
  int size = num-1;
  strncpy(to, from, size);
  if (strlen(from) >= size)
  {
   to[size] = '\0′;
  }   return to;
 }
 char* adstring_itoa(char* to, int from, int num)
 {
  char tmp[11];
  sprintf(tmp, "%d", from);
  adstring_strcpy(to, tmp, num);
  return to;
 }


 #ifdef __cplusplus
 }
 #endif

Saturday, July 26, 2008

Indoor Localization - Position System


This is the program I wrote for visualize indoor localization system.

I use GTK+2.x to implement GUI and the detail description is at the webpage of the program.

If there are any questions or suggestions, please feel free to email to me. Thanks

Program webpage: LINK

Friday, July 11, 2008

Complie MASE alpha version problem

It can be complied with pisa version, however there are some error inforamation pop out using alpha configuraiton.simplescalar

There are two files need to be midified

1.machine.h a. (line 223)
= orginal =
/* internal decoder state */
extern enum md_opcode md_mask2op[];
extern unsigned int md_opoffset[];
extern unsigned int md_opmask[];
extern unsigned int md_opshift[];

= change into =
extern enum md_opcode md_mask2op[MD_MAX_MASK+1];
extern unsigned int md_opoffset[OP_MAX];
extern unsigned int md_opmask[OP_MAX];
extern unsigned int md_opshift[OP_MAX];

b. codes below part a
/* global opcode names, these are returned by the decoder (MD_OP_ENUM()) */
enum md_opcode {
OP_NA = 0, /* NA */
#define DEFINST(OP,MSK,NAME,OPFORM,RES,FLAGS,O1,O2,I1,I2,I3) OP,
#define DEFLINK(OP,MSK,NAME,MASK,SHIFT) OP,
#define CONNECT(OP)
#include "machine.def"
OP_MAX /* number of opcodes + NA */ };

move them before part a

2.mae-mem.c (line 132)
= orginal =
static unsigned int /* total latency of access */

= change into =
unsigned int /* total latency of access */

There some bugs that have been discoveried.
Reference by:http://www.cc.gatech.edu/~loh/mase/

How to connect to wireless AP in text mode

INTERFACE_NAME: your wireless interface name, you can find it by "wconfig" command
AP_NAME : the ap you want to connect
AP_PASSWARD: the key for the AP_NAME

[root@localhost ~]# iwconfig INTERFACE_NAME essid "AP_NAME" key "AP_PASSWARD"

if the IP is accessed by DHCP
[root@localhost ~]# dhclient INTERFACE_NAME

Wednesday, June 18, 2008

GdkFont usage

To use GdkFont, you must specify the font name
And it must specify by a XLFD describing

The simplest way is set "-*-*-medium-r-normal--N-*-*-*-p-*-*-1"
N = the font size

mase simplescalar make error

There are two possible errors due to newer version of gcc.

1. /lib64/libc.so.6: could not read symbols: Bad value
( if the system is 32-bit, error might be /lib/libc.so.6: could not read symbols: Bad value )
  mark all "extern int errno;" ( eval.c misc.c range.c )
  and replace it by "#include "

2. mase-mem.c:134: error: static declaration of ‘mem_access_latency’ follows non-static declaration
mase-mem.h:104: error: previous declaration of ‘mem_access_latency’ was here
  find "mem_access_latency" in mase-mem.h and delete "static"

Tuesday, May 27, 2008

ns2 multicase problem in x86_64 system

I use ns-allinone-2.31 to build a ns2 enviroment in x86_64 linux system.
But there is a problem while running multicase in dense mode ( i don't this problem also happen in sparse mode). The multicased packets are flooding over the entire network.

The reason of this problem is in "MCastClassifier::classify(Packet *pkt)" in classifier-mcast.cc.

The original code is:
 if (p == 0) {
   // Didn't find an entry.
   tcl.evalf("%s new-group %d %d %d cache-miss", name(), src, dst, iface);

Try to change into:
 if (p == 0) {
   // Didn't find an entry.
   tcl.evalf("%s new-group %ld %ld %d cache-miss", name(), src, dst, iface);

Monday, May 19, 2008

ns2 nam編譯問題

使用ns2提供的allinone2.31.tar編譯時,nam會發生下面這個問題

......
nam_stream.o: In function `NamStreamCompressedFile::gets(char*, int)':
nam_stream.cc:(.text+0x1071): undefined reference to `gzgets'
nam_stream.o: In function `NamStreamCompressedFile::NamStreamCompressedFile(char const*)':
nam_stream.cc:(.text+0x10b4): undefined reference to `gzopen'
nam_stream.o: In function `NamStreamCompressedFile::NamStreamCompressedFile(char const*)':
nam_stream.cc:(.text+0x1136): undefined reference to `gzopen'
collect2: ld returned 1 exit status
make: *** [nam] Error 1

這個問題只要把nam資料夾下的Makefile稍作修改即可

LIB = \
-L/home/hct/ns-allinone-2.31/tclcl-1.19 -ltclcl -L/home/hct/ns-allinone-2.31/otcl -lotcl -L/home/hct/ns-allinone-2.31/lib -ltk8.4 -L/home/hct/ns-allinone-2.31/lib -ltcl8.4 -lz \

(紅字為增加部份)

Wednesday, May 14, 2008

Gtkmm under Windows using Microsoft Visual Stuido 2005

Gtkmm基本上算是GTK的延伸,差異在於他將GTK修改成C++的語法
在Visual Stuido 2005的安裝方法gtkmm_windows
首先要先下載Gtk+Gtkmm development package
若是使用vista的使用者,Visual Stuido 2005須先安裝sp1

照著網頁上的說明即可,不過我編譯的時候出現下列錯誤
LNK1104 : cannot open file 'cairo.lib'
不知道為什麼,明明我都沒有連結到cairo.lib.
最後我把安裝C:\GTK\lib\cairomm-1.0d.lib複製一份名稱改成cairo.lib
這樣就可以正常編譯執行,非常的神奇....

Friday, February 01, 2008

Installing TinyOS 2.0.2

因為我在Vista裝不起來TinyOS, 所以就把腦筋動到Linux下


http://www.tinyos.net/tinyos-2.x/doc/html/install-tinyos.html
照tinyos的網頁安裝即可,我使用的Linux是Suse 10.3
另外我是安裝TI MSP430 Tools
最後在home下的.bashrc加上
export CLASSPATH=$TOSROOT/support/sdk/java/tinyos.jar:.
export TOSROOT=/opt/tinyos-2.x
export TOSDIR=$TOSROOT/tos
export MAKERULES=$TOSROOT/support/make/Makerules
export PATH=/opt/msp430/bin:$PATH

最後如果在make telosb install.1 bel,/dev/ttyUSB0執行時發生can't not find serial
http://software.opensuse.org/search搜尋 python-serial 安裝後應該就可以成功

Saturday, January 05, 2008

VMware Failed to allocate page for guest RAM

當vmware的檔案是複製的時候
power on 可能會發生"Failed to allocate page for guest RAM"的錯誤

Solusion
在檔案所在目錄找到 *.vmx
並加入一行mainmem.UseNamedFile = "false"