C++ -malign-double 编译器标志
我需要一些有关 C++ 中编译器标志的帮助。我使用的库是从 Windows 到 Linux 的端口,必须使用 -malign-double 标志进行编译,“以实现 Win32 兼容性”。据我了解,这意味着我绝对也必须使用这个标志编译我自己的代码?其他 .so 共享库怎么样,它们是否也使用此标志重新编译?如果是这样,有什么办法解决这个问题吗?
我是 Linux 新手(和 C++),所以即使我尝试重新编译项目中使用的所有库,递归查找所有库及其依赖的库的源代码也太复杂了上,并重新编译一切。
编辑: 感谢您的回答。一些背景知识:该库控制对 USB 连接相机的初始化和访问。问题是,如果没有这个标志,奇怪的事情就会开始发生。看似随机,相机初始化失败,USB 连接错误。我还发现堆栈上的几个 c 字符串(const char*)出现某种内存损坏。基本上,在我调用该相机的初始化之前,它们指向一个目录路径;初始化后,它们指向字符串“me”。这对我来说非常令人困惑。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个伟大的旗帜名称!我想知道是否也有-malign-影响?但说实话,这个标志控制着轻微的优化:
库或您的代码不太可能需要此标志。一般来说,您不应该使用对齐控制标志,除非您确切知道自己在做什么。如果您确实觉得需要使用它们,请查阅 GCC 手册:http://gcc.gnu.org/onlinedocs 。
That's a great flag name! I wonder if there is also a -malign-influence? But seriously this flag controls a slight optimisation:
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.
您通常不需要更改现代编译器的对齐设置。
即使编译器存储一些未对齐的内容,程序也不会被破坏。
唯一需要它的地方是在 Linux 和 Windows 版本的程序之间以二进制形式传递的结构(通过文件或通过网络)。但在这些情况下,使用
pragma pack
是更好的风格。更新:驱动程序还要求二进制结构与规范逐位相等。
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.
可能是。如果该代码期望堆栈在入口处对齐,而您的代码不能确保这一点,则存在问题。堆分配的对象也是如此。如果您传递应该对齐的指针,但实际上没有对齐,那也是错误的。
同时,可能只有一两个函数需要对齐一些变量,但这毕竟不是问题。如果人们有足够的时间来理解他们负责的代码,那就太好了,但我想这并不是现实世界中的工作方式。
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.