tags:

views:

595

answers:

3

Hi,

probably this is a stupid question, but I am desperate now and can not finf the answer. In OSX during C++ program compilation with G++ I use

LD_FLAGS= -Wl,-stack_size,0x100000000

but in suse linux I get constantly erros like:

x86_64-suse-linux/bin/ld: unrecognized option '--stack'

and similar.

I know that some options to cope with this is 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?

Tjanks

A: 

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
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 informationcollect2: ld returned 1 exit status
asdf
Yes, edited my answer as I noticed that it doesn't work for ELF output. Sorry I'm not of much help here.
AndiDog
A: 

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
as I said, I can not do thatI have also to give this app to another users, who should not use this trick
asdf
You don't understand: the call to `setrlimit` can be done inside your C++ code, at the start of `main`.
FX
+2  A: 

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;
}
Paul R
error: ‘rlim_t’ does not name a type
Werner
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