views:

260

answers:

2

I know how to set the stack size to unlimited in the command line:

ulimit -s unlimited

And, in bash, when I set the stack size to unlimited, my code runs and terminates successfully.

Can I set the stack size to unlimited (or some specified size) in a makefile (with g++ as the compiler)? If so, how?

Note: I can only submit my source files (*.cpp, *.h) and a makefile for my project. That is, (1) the makefile is run, (2) the code is run. So, no scripts, or other special instructions are executed beforehand, which is why the changes must be made in the makefile...unless anyone has other/better/brilliant ideas?

Thanks in advance!

+2  A: 

You can do it for a per command basis:

target:
    ulimit -s unlimited && foo

This will allow foo to be run with an unlimited stack. Unfortunately, you will need to do add this before every command that needs the larger stack.

You can do something more general like this:

run_%: %
    ulimit -s unlimited && ./$^

# This will try to run a program called "progname" in the current
# directory with an unlimited stack
all: run_progname
R Samuel Klatchko
+3  A: 

Well, another brilliant idea to avoid the usage of ulimit (as invoking it as a separate application in your makefile (or by using scripts etc). This is by copying the functionality of the ulimit command by duplicating it in your own program (e.g. in your main).

In order to accomplish this, have a look at man 2 setrlimit ( manpage ), and take a look at RLIMIT_STACK and you probably want to set it to RLIM_INFINITY

amo-ej1