Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cmake change stack size

Is there a way to change stack size from the Cmake ?
I only found one forum thread mentioning CMAKE_CXX_STACK_SIZE but I couldn't find the documentation for this command. Ideally the command should work for both Visual Studio C++ and gcc.

like image 524
tommyk Avatar asked Apr 04 '18 06:04

tommyk


1 Answers

I don't have VS at the moment, but the following three CMake commands all work for me on MinGW/GCC (replace <target> with what you entered into add_executable()):

target_link_libraries(<target> PRIVATE "-Wl,--stack,10000000")

OR

set_target_properties(<target> PROPERTIES LINK_FLAGS -Wl,--stack,10000000)

OR

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,10000000")

Note that according to the CMake documentation, each of these should just add linker flags, not replace any that are already set.

In VS, it looks like you should replace -Wl,--stack, with /STACK: (more on this below) and use an if/else to have different commands for each compiler.

Regarding CMAKE_CXX_STACK_SIZE, this thread, which is worth a read, says the command is

in the implementation of the VS generator for historical reasons but is not intended as a first-class way to set the stack size. Instead just pass /STACK:... as a linker flag using target_link_libraries, or the LINK_FLAGS target property, or in CMAKE_EXE_LINKER_FLAGS...

Such a command can actually be seen on the page linked in your post (not sure if you saw it) as well as in this one:

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:10000000")
like image 170
Gumby The Green Avatar answered Nov 04 '22 02:11

Gumby The Green