Increasing stack size for C program in Clion

混江龙づ霸主 提交于 2019-12-11 10:23:30

问题


How can one increase the maximum size of a stack for C program that has a deep recursive call? Is there any config for the project where one can specify the stack or heap size for executables?


回答1:


To expand on the OP's own answer, the following three CMake commands all work for me on Windows with 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")

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

In Visual Studio, you should replace -Wl,--stack, with /STACK: in each of these according to this thread and others. For example:

target_link_libraries(<target> PRIVATE "/STACK:10000000")



回答2:


I solved the problem by adding following linker flag in project's CMakeList.txt

MATH(EXPR stack_size "16 * 1024 * 1024") # 16 Mb
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--stack,${stack_size}")



回答3:


To check initial stack size

peflags -x <binary>

to set size

peflags -x<size> <binary>

As reference peflags --help and
https://cygwin.com/ml/cygwin/2013-08/msg00318.html



来源:https://stackoverflow.com/questions/43326924/increasing-stack-size-for-c-program-in-clion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!