2009年2月19日星期四

netmate在x86_64平台编译遇到的问题和解决方案。

在x86_64平台上编译netmate时,会遇到以下错误信息:

...
../../src/include/stdincpp.h:46:22: error: multimap.h: No such file or directory
../../src/include/stdincpp.h:47:22: error: hash_map.h: No such file or directory
../../src/include/stdincpp.h:58:21: error: iomanip.h: No such file or directory
../../src/include/stdincpp.h:59:23: error: streambuf.h: No such file or directory
In file included from ../../src/netmate/FlowRecord.h:35,
from ../../src/include/ExportModuleInterface.h:39,
from ./ExportModule.h:34,
from testexp.cc:33:
../../src/netmate/MetricData.h: In member function 'char* MetricData::align(char*, DataType_e)':
../../src/netmate/MetricData.h:101: error: cast from 'char*' to 'unsigned int' loses precision
testexp.cc: In function 'char* getErrorMsg(int)':
testexp.cc:121: warning: deprecated conversion from string constant to 'char*'
testexp.cc: In function 'char* getModuleInfo(int)':
testexp.cc:127: warning: deprecated conversion from string constant to 'char*'
make[3]: *** [testexp.lo] 错误 1
...

g++版本和操作系统分别是gcc版本 4.3.2 (Ubuntu 4.3.2-1ubuntu12)或gcc version 4.1.2 20071124 (Red Hat 4.1.2-42)。

这个问题可以分为两部分:

  1. error: cast from 'char*' to 'unsigned int' loses precision
  2. 头文件找不到的问题。

error: cast from 'char*' to 'unsigned int' loses precision是由于指针的长度造成的,在传统32位系统中,指针的长度是32为,与int的长度相同,但是在64位Unix系统中,指针的长度已是64位(LP64)。所以char指针转换为unsigned int会出现问题。相关代码如下:

    inline char *align(char *var, DataType_e type)
{
return (DataTypeSize[type] == 0) ? var :
(char*) (((unsigned int)(var) + DataTypeSize[type]-1) /
DataTypeSize[type] * DataTypeSize[type]);
}

将其中的unsigned int修改为unsigned long,编译通过。

而第二个问题,在ubuntu 8.10中无法解决,这是libstdc++的版本造成的,在centos5中,安装compat-libstdc++-33即可解决问题。