更改 C++ 的堆栈大小使用 GNU 编译器编译期间在 Linux 中的应用程序
在 OSX 中,我使用 g++
编译 C++ 程序
LD_FLAGS= -Wl,-stack_size,0x100000000
,但在 SUSE Linux 中,我经常遇到类似的错误:
x86_64-suse-linux/bin/ld: unrecognized option '--stack'
和类似的错误。
我知道可以使用,
ulimit -s unlimited
但这并不好,因为单个用户并不总是可以做到这一点。
如何使用 GCC 增加 Linux 中单个应用程序的堆栈大小?
In OSX during C++ program compilation with g++
I use
LD_FLAGS= -Wl,-stack_size,0x100000000
but in SUSE Linux I constantly get errors like:
x86_64-suse-linux/bin/ld: unrecognized option '--stack'
and similar.
I know that it is possible to use
ulimit -s unlimited
but this is not nice as not always can a single user do that.
How can I increase the stack size in Linux with GCC for a single application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 setrlimit 以编程方式设置堆栈大小,例如
注意:即使使用此方法增加堆栈大小,您不应该在
main()
本身中声明大型局部变量,因为一旦您进入main()
,在 < code>getrlimit/setrlimit
代码有机会更改堆栈大小。因此,任何大型局部变量都应该仅在堆栈大小成功增加后随后从main()
调用的函数中定义。You can set the stack size programmatically with setrlimit, e.g.
Note: even when using this method to increase stack size you should not declare large local variables in
main()
itself, since you may well get a stack overflow as soon as you entermain()
, before thegetrlimit
/setrlimit
code has had a chance to change the stack size. Any large local variables should therefore be defined only in functions which are subsequently called frommain()
, after the stack size has successfully been increased.不要使用
stack_size
,而是使用--stack
,如下所示:此示例应为您提供 4 MB 的堆栈空间。适用于 MinGW 的 GCC,但正如联机帮助页所述,“此选项特定于链接器的 i386 PE 目标端口”(即仅适用于输出 Windows 二进制文件)。似乎没有 ELF 二进制文件的选项。
Instead of
stack_size
, use--stack
like so:This example should give you 4 MB of stack space. Works on MinGW's GCC, but as the manpage says, "This option is specific to the i386 PE targeted port of the linker" (i.e. only works for outputting Windows binaries). Seems like there isn't an option for ELF binaries.
这是一个老话题,但这里回答的所有标志都不适合我。无论如何,我发现
-Wl,-z,stack-size=4194304
(4MB 的示例)似乎有效。This is an old topic, but none of the flags answered here worked for me. Anyway by I found out that
-Wl,-z,stack-size=4194304
(example for 4MB) seems to work.考虑使用
-fsplit-stack
选项 https://gcc.gnu.org/wiki /拆分堆栈Consider using
-fsplit-stack
option https://gcc.gnu.org/wiki/SplitStacks使用 ulimit bash 内置函数或 setrlimit() 或在登录时更改它
使用 PAM (pam_limits.so)。
这是一个可设置的
用户资源限制;请参阅 setrlimit(2) 中的 RLIMIT_STACK。
http://bytes.com/topic/c/answers/ 221976-enlarge-stack-size-gcc
Change it with the ulimit bash builtin, or setrlimit(), or at login
with PAM (pam_limits.so).
It's a settable
user resource limit; see RLIMIT_STACK in setrlimit(2).
http://bytes.com/topic/c/answers/221976-enlarge-stack-size-gcc