C ++ -malign-double compiler flag

I need help on compiler flags in C ++. I am using a library, which is a port for linux from Windows, which must be compiled with the -malign-double flag, "for compatibility with Win32". As far as I understand, this means that I also have to compile my own code with this flag? What about other .so shared libraries, are they also recompiled with this flag? If so, is there any way around this?

I am new to linux (and C ++), so although I tried to recompile all the libraries that I use for my project, it was too difficult to recursively find the source for all the libraries and libraries that they depend on and recompile everything.

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

+4
source share
3 answers

Usually you do not need to change the alignment settings for modern compilers. Even if the compiler remains inconsistent, the program will not be broken.

The only place where this may be needed is those that are transmitted between the linux and windows versions in binary format (via files or via a network). But in these cases, using the pragma pack is the best style.

Update. Drivers also require binary structures to be beat equal with the specification.

+2
source

This is a great flag name! I wonder if there is also a personal influence? But seriously, this flag controls a little optimization:

-malign double

-mno-align-double

Control whether GCC aligns double, long double, and long long variables at the border of two words or the border of one word. variables at the border of two words will produce encoding performed somewhat faster on the "Pentium" due to more memory.

It is unlikely that a library or code is needed for this flag. In general, you should not use alignment control flags unless you know exactly what you are doing. If you feel that you need to use them, refer to the GCC manual at http://gcc.gnu.org/onlinedocs .

+4
source

It could be. If this code expects a stack alignment as you type, and your code does not guarantee that there is a problem. The same applies to selected heaps of objects. If you skip pointers that should be aligned, but that is not the case.

At the same time, it may be that only one or two functions require some variables to be aligned, and this has never been a problem. It would be nice if people were given the time needed to understand the code for which they are responsible, but I think that is not how it works in the real world.

0
source

Source: https://habr.com/ru/post/1304298/


All Articles