65

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?

Palec
  • 12,743
  • 8
  • 69
  • 138
asdf
  • 2,281
  • 5
  • 28
  • 29
  • if it helps: 2.6.18.8-0.9-default #1 SMP Sun Feb 10 22:48:05 UTC 2008 x86_64 x86_64 x86_64 GNU/Linux – asdf Feb 16 '10 at 19:16
  • gcc --ver: gcc version 4.1.2 20061115 (prerelease) (SUSE Linux) – asdf Feb 16 '10 at 19:17
  • Attempting to set `rlimit_stack` after [Stack Clash](http://www.openwall.com/lists/oss-security/2017/06/19/1) remediations may result in failure or related problems. Also see Red Hat [Issue 1463241](https://bugzilla.redhat.com/show_bug.cgi?id=1463241) – jww Jun 21 '17 at 16:30
  • `ld -v`, please – Sergei Krivonos Feb 23 '21 at 00:33

5 Answers5

79

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.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Make sure you have the correct `#include` s for your OS, e.g. for Mac OS X it would be `#include `. – Paul R Jul 09 '10 at 16:32
  • I tried it on SLES 11. Even the value is set I cannot define a local variable according to the new limit --> `segmentation fault`. Only when I run in the command line `ulimit -s 16000` then I can define a local variable like `char x [14000000]`. Apparently the stack size is not set through this code. Any idea? – Peter VARGA Apr 12 '15 at 20:31
  • 1
    @AlBundy: all I can tell you is that the above code works for me on both OS X and various flavours of Linux. One thought occurs to me though: are you trying to declare a large local variable in `main()` itself ? That won't work of course, because you'll get a stack overflow before you even get to the `getrlimit`/`setrlimit` code. Put the large local variable in another function and then call this function from `main` (after the `getrlimit`/`setrlimit` code). – Paul R Apr 12 '15 at 22:15
  • 1
    @PaulR: OK. I tried it in `main()` but __after__ I set the new limit. Apparently this does not work. When I call from `main()` a function which has a huge local variable then it works. I checked it now with different limits and all works perfectly. Conclusion: The stack limit must be set in `main()` in order to be active for the rest of the program. I had also to close the `ssh` session because I was playing around with `ulimit -s`and I got always -1 as return code. – Peter VARGA Apr 12 '15 at 22:27
  • Thanks for confirming that - I've added a note to the answer now in case anyone else runs into the same problem. – Paul R Apr 12 '15 at 22:29
  • @PaulR: The documentation says: "_The maximum size of the process stack, in bytes_". This means all threads are sharing the stack and when my program has up to 200 threads I have to increase the stack size with the method and then all should be OK? – Peter VARGA Apr 12 '15 at 22:40
  • @AlBundy: each thread will still get the default stack size for a thread (although you can easily change this of course), but by increasing the process stack size you'll be able to create more threads, if that's what you mean. – Paul R Apr 12 '15 at 22:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75086/discussion-between-al-bundy-and-paul-r). – Peter VARGA Apr 13 '15 at 07:42
  • This just causes my program to freeze. – Larrimus Oct 25 '17 at 01:53
23

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.

AndiDog
  • 68,631
  • 21
  • 159
  • 205
  • 3
    unfortunately I tried this and it does not work: /usr/lib64/gcc/x86_64-suse-linux/4.1.2/../../../../x86_64-suse-linux/bin/ld: unrecognized option '--stack' /usr/lib64/gcc/x86_64-suse-linux/4.1.2/../../../../x86_64-suse-linux/bin/ld: use the --help option for usage information collect2: ld returned 1 exit status – asdf Feb 16 '10 at 19:16
  • Yes, edited my answer as I noticed that it doesn't work for ELF output. Sorry I'm not of much help here. – AndiDog Feb 16 '10 at 19:17
  • 3
    Well, you saved me. I'm on windows ;). Thanks! – Jan Święcki Mar 03 '13 at 21:13
  • In CMake, there are [a few commands](https://stackoverflow.com/a/55989674) that set this option, which all work for me on Windows. – Gumby The Green May 05 '19 at 07:30
12

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.

yugr
  • 19,769
  • 3
  • 51
  • 96
Rof
  • 121
  • 1
  • 2
7

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

Sergei Krivonos
  • 4,217
  • 3
  • 39
  • 54
2

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

f0ster
  • 549
  • 3
  • 12
  • as I said, I can not do that I have also to give this app to another users, who should not use this trick – asdf Feb 16 '10 at 19:15
  • 4
    You don't understand: the call to `setrlimit` can be done inside your C++ code, at the start of `main`. – F'x Feb 16 '10 at 20:17