宅男的周末

最近在进行把linux项目移植到windows下的工作,发现不少有趣的事情:比如因为windows跟*nix的回车符格式不一,文件存取要记得ios::binary。
还有就是该死的内存对齐问题,为了能够顺利读取之前在linux下保存的项目文件,需要mingw的编译器跟linux下的g++保持一致,所有struct前后需加上#pragma pack (push,1) 跟 #pragma pack(pop),表示向1字节对齐;而如果该struct在某个class内部定义,则需要向4字节对齐,#pragma pack (push,4)。
 
一直想写个systemfunctions库,把linux跟windows下不同的系统函数统一wrap一下,方便项目在linux跟windows下移植。上个周末把与时间有关的函数都处理了,下面需要补充一下thread函数,不过这个我暂时不想动,毕竟也有pthread for win32顶着嘛。
 
突然对socket挺感兴趣,这一块一直没有弄透。虽然自己之前把老板写的libsocket移植到了windows,但总觉得写得不大好,想重新写一个,同时支持IPv6。昨天发现windows xp下面缺了不少linux下有的函数,真麻烦,还得看看winsock2。于是暂时作罢,下周吧。
 
该死的vs2005,硬要把它自己的动态库msvcr80.dll塞给客户,visual studio编译出来的程序就一定不能摆脱dll么,linux就一个可执行文件到处可以运行多好啊。(当然别碰上glibc 2.3或之前的老系统)
 
前两周Coin3D 2.5.0在mingw下一直没编译成功,libtool 老说 link error object name conflicts。自己也不懂libtool,今天狠下心来看libtool代码,发现原来根本是误报,实际情况是sort命令出错了,而不是什么object name conflicts。sh里的sort命令不认任何参数,觉得真奇怪,以为msys没移植好这个命令。手动更改libtool代码后,又出现FIND:  Parameter format not correct。 find命令也不认任何参数,这事太奇怪了,用which一查发现原来windows/system32也有find.exe跟sort.exe,晕倒。看来系统变量PATH里得把msys/bin放在windows/system32之前才行。我之前玩了这么久msys居然发现任何问题。当然可能也跟Coin3D本身没打算支持mingw有关,要不然这个问题还不知啥时候才暴露哇。
 
接下来编译SoQt 1.4.1,SoQt也没想支持mingw,自动生成的msvc项目会定义COIN_DLL 或COIN_NOT_DLL,而mingw下则需要手动加上configure参数CPPFLAGS="-DCOIN_DLL" 。
此条目发表在Uncategorized分类目录。将固定链接加入收藏夹。

5 Responses to 宅男的周末

  1. yueting说道:

    To illustrate the structure padding problem, consider this C declaration: struct foo {
    char a;
    int b;
    char c;
    } foo_instance;

    Assuming 32-bit ints, you might guess that the structure occupies 6 bytes, but this is not so. For efficiency reasons, compilers "pad" structures to align the data members in a way that is convenient for the CPU. Most CPUs can access 32-bit integers faster if they are at addresses evenly divisible by 4, so the above structure would probably take up 12 bytes on these systems. This issue rears its head when you try to send a structure over Winsock whole, like this: send(sd, (char*)&foo_instance, sizeof(foo), 0);

    Unless the receiving program was compiled on the same machine architecture with the same compiler and the same compiler options, you have no guarantee that the other machine will receive the data correctly.
    The solution is to always send structures "packed" by sending the data members one at a time. You can force your compiler to pack the structures for you, with a resulting speed penalty in the code that accesses those structures. Visual C++ can do this with the /Zp command line option or the #pragma pack directive, and Borland C++ can do this with the -a command line option. Keep the byte ordering problem in mind, however: if you send a packed structure in place, be sure to reorder its bytes properly before you send it.

  2. 说道:

    其实能读懂你的这篇博文也是需要技术含量的。。。我就没有。。。

  3. 说道:

    已然看不懂了。。。

  4. Jun说道:

    一年半之后再次在mingw下编译coin3d 3.1.3 出现libtool问题,google之,发现自己的blog

  5. Jun说道:

    编译Qt的static库,configure.exe -release -static -no-exceptions -no-qt3support -no-webkit -no-phonon

留下评论