views:

273

answers:

3

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.

+5  A: 

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.

anon
+3  A: 

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.

osgx
I think this may be it. The makefile for the library mentioned something about struct alignment. So I could potentially add this pragma pack directive to every struct the library that "needs" the alignment? Since I'm using linux gcc, this directive doesn't seem to be available though.
Martin
@Martin GCC does support packing pragmas - please read the manual I gave a link to in my answer, section 5.53.7. Whether or not this will solve your problem, I couldn't say.
anon
@Martin, USB driver does use binary structs. It is used as special file, and program must generate bit-exact commands to web-camera.You do need align file only for libraries, which generate bit data for this usb device.
osgx
A: 

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.

jdv