在OS X上使用Clang进行编译时如何增加堆栈大小?

Rob*_*lan 13 macos ld stack-size compiler-options

我可以用clang ++指定堆栈大小吗?我找不到任何允许我这样做的编译器选项.我正在使用OS X.

注意:这个问题具体是指Clang,而不是GCC编译器.

bda*_*ash 17

链接器而不是编译器负责设置主线程的堆栈大小.手册页ld包含以下内容:

-stack_size size
    Specifies the maximum stack size for the main thread in a program. Without this
    option a program has a 8MB stack. The argument size is a hexadecimal number with
    an optional leading 0x. The size should be an even multiple of 4KB, that is the
    last three hexadecimal digits should be zero.
Run Code Online (Sandbox Code Playgroud)

例如,要指定16MB堆栈,您可以执行以下操作:

mrowe@apollo:~$ cc -Wl,-stack_size -Wl,0x1000000 -o test test.m
mrowe@apollo:~$ otool -lV test | grep stack
 stacksize 16777216
Run Code Online (Sandbox Code Playgroud)

请注意-Wl,传递给cc它的参数的前缀,以便将它们传递给链接器.