C++ -malign-double compiler flag

心不动则不痛 提交于 2019-12-01 20:52:13

问题


I need some help on compiler flags in c++. I'm using a library that is a port to linux from windows, that has to be compiled with the -malign-double flag, "for Win32 compatibility". It's my understanding that this mean I absolutely have to compile my own code with this flag as well? How about other .so shared libraries, do they have be recompiled with this flag as well? If so, is there any way around this?

I'm a linux newbie (and c++), so even though I tried to recompile all the libraries I'm using for my project, it was just too complicated to recursively find the source for all the libraries and the libraries they're dependent on, and recompile everything.

Edit: Thanks for the answers. Some background: This library controls the initialization and access to a USB-connected camera. The problem is that without this flag, weird things start to happen. Seemingly at random, the initialization of the camera fails, with USB connection errors. I also get some kind of memory corruption of several c-strings (const char*) that are on the stack. Basically, before I call the initialization of this camera, they point to a directory path; after the initialization, they point to the string "me". Which to me is very confusing.


回答1:


You usually dont need to change alignment settings for modern compilers. Even if compiler will store someting unaligned, program will be not broken.

The only place where it can be needed is stuctures passed between linux and windows version of programm in binary (via files or via network). But in these cases the usage of pragma pack is better style.

Update: drivers also require binary structures to be bit-by-bit equal with specification.




回答2:


That's a great flag name! I wonder if there is also a -malign-influence? But seriously this flag controls a slight optimisation:

-malign-double

-mno-align-double

Control whether GCC aligns double, long double,and long long variables on a two word boundary or a one word boundary.Aligning double variables on a two word boundary will produce codethat runs somewhat faster on a ‘Pentium’ at the expense of more memory.

It's unlikely that either the library or your code needs this flag. In general, you should not be using alignment control flags, unless you know exactly what you are doing. If you do feel you need to use them, consult the GCC manual at http://gcc.gnu.org/onlinedocs.




回答3:


It could be. If that code expects the stack to be aligned on entry, and your code doesn't ensure that, there is a problem. The same goes for heap allocated objects. If you pass pointers that should be aligned, but aren't, that's wrong too.

At the same time, it could be that just one or two functions require some variables to be aligned, and it never was a problem after all. It would be nice if people are given the time required to understand the code they are responsible for but I guess that's not how it works in the real world.



来源:https://stackoverflow.com/questions/2457182/c-malign-double-compiler-flag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!