70

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?

4
  • 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, 2010 at 19:16
  • gcc --ver: gcc version 4.1.2 20061115 (prerelease) (SUSE Linux)
    – asdf
    Feb 16, 2010 at 19:17
  • Attempting to set rlimit_stack after Stack Clash remediations may result in failure or related problems. Also see Red Hat Issue 1463241
    – jww
    Jun 21, 2017 at 16:30
  • ld -v, please Feb 23, 2021 at 0:33

5 Answers 5

82

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.

9
  • Make sure you have the correct #include s for your OS, e.g. for Mac OS X it would be #include <sys/resource.h>.
    – Paul R
    Jul 9, 2010 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? Apr 12, 2015 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, 2015 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 -sand I got always -1 as return code. Apr 12, 2015 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, 2015 at 22:29
25

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.

3
  • 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, 2010 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, 2010 at 19:17
  • In CMake, there are a few commands that set this option, which all work for me on Windows. May 5, 2019 at 7:30
13

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.

7

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

2
  • 3
    -fsplit-stack decreased performance by ~20% for my use case.
    – Dennis
    Dec 10, 2018 at 23:56
  • 2
    Hi @Dennis, consider comparing this performance to use of heap. Feb 24, 2020 at 10:05
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

2
  • 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, 2010 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, 2010 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.