embeded/Cortex-M3 STM

stm32f103c8t6 cpp std::cout 실패

구차니 2026. 2. 18. 22:14

아래코드까진 문제가 없는데

class my_class
{
public:
	int i_var;
	int f();
};

int my_class::f()
{
	return ++i_var;
}

int main(void)
{
	my_class *p_my_class = new my_class();
	p_my_class->f();
}

 

iostream을 추가해서 (아직 uart 출력을 연결하진 않고 빌드만 했지만)

std::cout을 통해 출력만 해보려고 했는데

#include <iostream>

class my_class
{
public:
	int i_var;
	int f();
};

int my_class::f()
{
	return ++i_var;
}

int main(void)
{
	my_class *p_my_class = new my_class();
	std::cout << p_my_class->f();
}

 

플래시 용량이 터져나가 버렸다.

cout을 쓰려면 최소 192KB 이상의 flash는 되어야 한다는건가?

22:05:02 **** Incremental Build of configuration Debug for project cpp_test ****
make -j4 all 
arm-none-eabi-g++ "../Core/Src/main.cpp" -mcpu=cortex-m3 -std=gnu++14 -g3 -DDEBUG -DUSE_HAL_DRIVER -DSTM32F103xB -c -I../Core/Inc -I../Drivers/STM32F1xx_HAL_Driver/Inc/Legacy -I../Drivers/STM32F1xx_HAL_Driver/Inc -I../Drivers/CMSIS/Device/ST/STM32F1xx/Include -I../Drivers/CMSIS/Include -O0 -ffunction-sections -fdata-sections -fno-exceptions -fno-rtti -fno-use-cxa-atexit -Wall -fstack-usage -fcyclomatic-complexity -MMD -MP -MF"Core/Src/main.d" -MT"Core/Src/main.o" --specs=nano.specs -mfloat-abi=soft -mthumb -o "Core/Src/main.o"
arm-none-eabi-g++ -o "cpp_test.elf" @"objects.list"   -mcpu=cortex-m3 -T"/home/minimonk/STM32CubeIDE/workspace_1.19.0_cpp/cpp_test/STM32F103C8TX_FLASH.ld" --specs=nosys.specs -Wl,-Map="cpp_test.map" -Wl,--gc-sections -static --specs=nano.specs -mfloat-abi=soft -mthumb -Wl,--start-group -lc -lm -lstdc++ -lsupc++ -Wl,--end-group
/home/minimonk/st/stm32cubeide_1.19.0/plugins/cohttp://m.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.13.3.rel1.linux64_1.0.0.202410170706/tools/bin/../lib/gcc/arm-none-eabi/13.3.1/../../../../arm-none-eabi/bin/ld: /home/minimonk/st/stm32cubeide_1.19.0/plugins/cohttp://m.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.13.3.rel1.linux64_1.0.0.202410170706/tools/bin/../lib/gcc/arm-none-eabi/13.3.1/../../../../arm-none-eabi/lib/thumb/v7-m/nofp/libc_nano.a(libc_a-getentropyr.o): in function `_getentropy_r':
(.text._getentropy_r+0xe): warning: _getentropy is not implemented and will always fail
/home/minimonk/st/stm32cubeide_1.19.0/plugins/cohttp://m.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.13.3.rel1.linux64_1.0.0.202410170706/tools/bin/../lib/gcc/arm-none-eabi/13.3.1/../../../../arm-none-eabi/bin/ld: /home/minimonk/st/stm32cubeide_1.19.0/plugins/cohttp://m.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.13.3.rel1.linux64_1.0.0.202410170706/tools/bin/../lib/gcc/arm-none-eabi/13.3.1/../../../../arm-none-eabi/lib/thumb/v7-m/nofp/libc_nano.a(libc_a-getentropyr.o): note: the message above does not take linker garbage collection into account
/home/minimonk/st/stm32cubeide_1.19.0/plugins/cohttp://m.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.13.3.rel1.linux64_1.0.0.202410170706/tools/bin/../lib/gcc/arm-none-eabi/13.3.1/../../../../arm-none-eabi/bin/ld: cpp_test.elf section `.text' will not fit in region `FLASH'
/home/minimonk/st/stm32cubeide_1.19.0/plugins/cohttp://m.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.13.3.rel1.linux64_1.0.0.202410170706/tools/bin/../lib/gcc/arm-none-eabi/13.3.1/../../../../arm-none-eabi/bin/ld: region `FLASH' overflowed by 109220 bytes
collect2: error: ld returned 1 exit status
make: *** [makefile:88: cpp_test.elf] Error 1
"make -j4 all" terminated with exit code 2. Build might be incomplete.

22:05:04 Build Failed. 4 errors, 1 warnings. (took 2s.252ms)

[링크 : https://igotit.tistory.com/entry/STM32CubeIDE-임베디드-코드에서-C-클래스-활용]

 


gpt 말로는 아래를 설정해주라는데, std::cout 하면 버퍼부터 하위시스템 다 끌려온다는거 치고는 너무 큰디..

데드 코드 삭제
-ffunction-sections
-fdata-sections

링커옵션
-Wl,--gc-sections


예외/RTTI 끄기
-fno-exceptions
-fno-rtti

newlib nano
--specs=nano.specs

 

그래서 설정을 보는데, 기본으로 부동소수점 출력/입력은 안하게 되어있고

 

g++ 쪽은 기본적으로 아래의 4개 플래그가 설정되어 있다.

-ffunctions-sections

-fdata-sections

-fno-exceptions

-fno-rtti

 

링커도 -Wl,--gc-sections 되어 있어서 더 옵션으로 먼가 줄일수 있는 상태는 아닌데..

[링크 : https://vuzwa.tistory.com/entry/STM32CubeIDE에서-개발환경-C-or-C로-전환하기]

 

그래서 std::cout 은 포기하고 printf나 쓰라고 하는건가?

그런데 rtti 까지 사용하지 않으면 cpp를 쓰는 이유가 머가 남지 -ㅁ-?