更改 C++ 的堆栈大小使用 GNU 编译器编译期间在 Linux 中的应用程序

发布于 2024-08-22 03:59:26 字数 497 浏览 7 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吲‖鸣 2024-08-29 03:59:27

您可以使用 setrlimit 以编程方式设置堆栈大小,例如

#include <sys/resource.h>

int main (int argc, char **argv)
{
    const rlim_t kStackSize = 16 * 1024 * 1024;   // min stack size = 16 MB
    struct rlimit rl;
    int result;

    result = getrlimit(RLIMIT_STACK, &rl);
    if (result == 0)
    {
        if (rl.rlim_cur < kStackSize)
        {
            rl.rlim_cur = kStackSize;
            result = setrlimit(RLIMIT_STACK, &rl);
            if (result != 0)
            {
                fprintf(stderr, "setrlimit returned result = %d\n", result);
            }
        }
    }

    // ...

    return 0;
}

注意:即使使用此方法增加堆栈大小,您不应该在 main() 本身中声明大型局部变量,因为一旦您进入 main(),在 < code>getrlimit/setrlimit 代码有机会更改堆栈大小。因此,任何大型局部变量都应该仅在堆栈大小成功增加后随后从 main() 调用的函数中定义。

You can set the stack size programmatically with setrlimit, e.g.

#include <sys/resource.h>

int main (int argc, char **argv)
{
    const rlim_t kStackSize = 16 * 1024 * 1024;   // min stack size = 16 MB
    struct rlimit rl;
    int result;

    result = getrlimit(RLIMIT_STACK, &rl);
    if (result == 0)
    {
        if (rl.rlim_cur < kStackSize)
        {
            rl.rlim_cur = kStackSize;
            result = setrlimit(RLIMIT_STACK, &rl);
            if (result != 0)
            {
                fprintf(stderr, "setrlimit returned result = %d\n", result);
            }
        }
    }

    // ...

    return 0;
}

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 enter main(), before the getrlimit/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 from main(), after the stack size has successfully been increased.

掌心的温暖 2024-08-29 03:59:27

不要使用 stack_size,而是使用 --stack,如下所示:

gcc -Wl,--stack,4194304 -o program program.c

此示例应为您提供 4 MB 的堆栈空间。适用于 MinGW 的 GCC,但正如联机帮助页所述,“此选项特定于链接器的 i386 PE 目标端口”(即仅适用于输出 Windows 二进制文件)。似乎没有 ELF 二进制文件的选项。

Instead of stack_size, use --stack like so:

gcc -Wl,--stack,4194304 -o program program.c

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.

迷爱 2024-08-29 03:59:27

这是一个老话题,但这里回答的所有标志都不适合我。无论如何,我发现 -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.

洋洋洒洒 2024-08-29 03:59:27

考虑使用 -fsplit-stack 选项 https://gcc.gnu.org/wiki /拆分堆栈

Consider using -fsplit-stack option https://gcc.gnu.org/wiki/SplitStacks

拥抱影子 2024-08-29 03:59:27

使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文