Skip to main content
JuniorSTM
Associate
April 30, 2021
Solved

#include into cpp header file

  • April 30, 2021
  • 3 replies
  • 12742 views

Hello everyone, I have a problem with include string methods into cpp header file. I created .cpp files like in this example: https://dev.to/younup/cmake-on-stm32-episode-5-get-ready-for-c-1mh9

Also, I generated the project with c++ checkbox.

When I #include <string> in app.cpp file I can create a string, but I want to include the library in app.hpp file I get an error. Therefore I can't declare the prototypes of functions in the header file. I'm using Nucleo-L152re board. Codes and errors below.

I will be grateful for any help ;) 

//app.hpp
#pragma once
#ifndef _APP_H_
#define _APP_H_
 
#include "stm32l1xx_hal.h"
#include "main.h"
 
//#include <string> // <- Error 1
 
#ifdef __cplusplus
extern "C"
{
#endif
//#include <string> // <- Error 2
 
 
void setup();
void loop();
 
//void testString(std::string text = "abc");
 
//std::string alp = "";
 
#ifdef __cplusplus
}
#endif
 
#endif
//app.cpp
#include "app.hpp"
 
//#include <string>
 
extern "C" {
 
void setup() {
 // For future episodes ;)
}
 
void loop() {
	HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
 HAL_Delay(200U);
 
}
 
//void testString(std::string text) {} // <- this works when library inc. in this file
 
 
}
//Error 1 /
 
21:41:30 **** Incremental Build of configuration Debug for project skytrap ****
make -j8 all 
arm-none-eabi-g++ "../Core/Src/app.cpp" -mcpu=cortex-m3 -std=gnu++14 -g3 -DUSE_HAL_DRIVER -DSTM32L152xE -DDEBUG -c -I../Core/Inc -I../Drivers/STM32L1xx_HAL_Driver/Inc -I../Drivers/STM32L1xx_HAL_Driver/Inc/Legacy -I../Drivers/CMSIS/Device/ST/STM32L1xx/Include -I../Drivers/CMSIS/Include -O0 -ffunction-sections -fdata-sections -fno-exceptions -fno-rtti -fno-threadsafe-statics -fno-use-cxa-atexit -Wall -fstack-usage -MMD -MP -MF"Core/Src/app.d" -MT"Core/Src/app.o" --specs=nano.specs -mfloat-abi=soft -mthumb -o "Core/Src/app.o"
arm-none-eabi-gcc "../Core/Src/main.c" -mcpu=cortex-m3 -std=gnu11 -g3 -DUSE_HAL_DRIVER -DSTM32L152xE -DDEBUG -c -I../Core/Inc -I../Drivers/STM32L1xx_HAL_Driver/Inc -I../Drivers/STM32L1xx_HAL_Driver/Inc/Legacy -I../Drivers/CMSIS/Device/ST/STM32L1xx/Include -I../Drivers/CMSIS/Include -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage -MMD -MP -MF"Core/Src/main.d" -MT"Core/Src/main.o" --specs=nano.specs -mfloat-abi=soft -mthumb -o "Core/Src/main.o"
In file included from ../Core/Src/main.c:21:0:
../Core/Inc/app.hpp:8:10: fatal error: string: No such file or directory
 #include <string> // <- Error 1
 ^~~~~~~~
compilation terminated.
make: *** [Core/Src/subdir.mk:53: Core/Src/main.o] Error 1
make: *** Waiting for unfinished jobs....
"make -j8 all" terminated with exit code 2. Build might be incomplete.
 
21:41:32 Build Failed. 3 errors, 0 warnings. (took 2s.410ms)
 
//Error 2 {~400kb file}
 
21:42:08 **** Incremental Build of configuration Debug for project skytrap ****
make -j8 all 
arm-none-eabi-g++ "../Core/Src/app.cpp" -mcpu=cortex-m3 -std=gnu++14 -g3 -DUSE_HAL_DRIVER -DSTM32L152xE -DDEBUG -c -I../Core/Inc -I../Drivers/STM32L1xx_HAL_Driver/Inc -I../Drivers/STM32L1xx_HAL_Driver/Inc/Legacy -I../Drivers/CMSIS/Device/ST/STM32L1xx/Include -I../Drivers/CMSIS/Include -O0 -ffunction-sections -fdata-sections -fno-exceptions -fno-rtti -fno-threadsafe-statics -fno-use-cxa-atexit -Wall -fstack-usage -MMD -MP -MF"Core/Src/app.d" -MT"Core/Src/app.o" --specs=nano.specs -mfloat-abi=soft -mthumb -o "Core/Src/app.o"
arm-none-eabi-gcc "../Core/Src/main.c" -mcpu=cortex-m3 -std=gnu11 -g3 -DUSE_HAL_DRIVER -DSTM32L152xE -DDEBUG -c -I../Core/Inc -I../Drivers/STM32L1xx_HAL_Driver/Inc -I../Drivers/STM32L1xx_HAL_Driver/Inc/Legacy -I../Drivers/CMSIS/Device/ST/STM32L1xx/Include -I../Drivers/CMSIS/Include -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage -MMD -MP -MF"Core/Src/main.d" -MT"Core/Src/main.o" --specs=nano.specs -mfloat-abi=soft -mthumb -o "Core/Src/main.o"
In file included from ../Core/Src/main.c:21:0:
../Core/Inc/app.hpp:14:10: fatal error: string: No such file or directory
 #include <string> // <- Error 2
 ^~~~~~~~
compilation terminated.
make: *** [Core/Src/subdir.mk:53: Core/Src/main.o] Error 1
make: *** Waiting for unfinished jobs....
In file included from c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\bits\stringfwd.h:40:0,
 from c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\string:39,
 from ../Core/Inc/app.hpp:14,
 from ../Core/Src/app.cpp:1:
c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\bits\memoryfwd.h:63:3: error: template with C linkage
 template<typename>
 ^~~~~~~~
c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\bits\memoryfwd.h:66:3: error: template specialization with C linkage
 template<>
 ^~~~~~~~
c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\bits\memoryfwd.h:70:3: error: template with C linkage
 template<typename, typename>
 ^~~~~~~~
In file included from c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\string:39:0,
 from ../Core/Inc/app.hpp:14,
 from ../Core/Src/app.cpp:1:
c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\bits\stringfwd.h:52:3: error: template with C linkage
 template<class _CharT>
 ^~~~~~~~
c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\bits\stringfwd.h:55:3: error: template specialization with C linkage
 template<> struct char_traits<char>;
 ^~~~~~~~
c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\bits\stringfwd.h:58:3: error: template specialization with C linkage
 template<> struct char_traits<wchar_t>;
 ^~~~~~~~
c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\bits\stringfwd.h:63:3: error: template specialization with C linkage
 template<> struct char_traits<char16_t>;
 ^~~~~~~~
c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\bits\stringfwd.h:64:3: error: template specialization with C linkage
 template<> struct char_traits<char32_t>;
 ^~~~~~~~
c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\bits\stringfwd.h:69:3: error: template with C linkage
 template<typename _CharT, typename _Traits = char_traits<_CharT>,
 ^~~~~~~~
In file included from c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\bits\stl_algobase.h:63:0,
 from c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\bits\char_traits.h:39,
 from c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\string:40,
 from ../Core/Inc/app.hpp:14,
 from ../Core/Src/app.cpp:1:
c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\ext\numeric_traits.h:54:3: error: template with C linkage
 template<typename _Value>
 ^~~~~~~~
 
////// MORE AND MORE ------------------------------------------------------------------------
 
c:\st\stm32cubeide_1.4.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.7-2018-q2-update.win32_1.4.0.202007081208\tools\arm-none-eabi\include\c++\7.3.1\bits\basic_string.tcc:1535:3: error: template with C linkage
 template<typename _CharT, typename _Traits, typename _Alloc>
 ^~~~~~~~
make: *** [Core/Src/subdir.mk:49: Core/Src/app.o] Error 1
"make -j8 all" terminated with exit code 2. Build might be incomplete.
 
21:42:12 Build Failed. 1134 errors, 0 warnings. (took 3s.349ms)

This topic has been closed for replies.
Best answer by TDK

<string> is for std::string objects. <cstring> and <string.h> are for strlen() and similar functions.

http://www.cplusplus.com/reference/string/string/

3 replies

TDK
Super User
April 30, 2021

You can't include a C++ header in a C file. Easiest solution would be to rename main.c to main.cpp.

"If you feel a post has answered your question, please click ""Accept as Solution""."
JuniorSTM
JuniorSTMAuthor
Associate
April 30, 2021

Is any possible to setting header files to include C++ methods? When I include a library in .cpp file everything works fine, but the headers file really doesn't want to be treated as C++ files... I tried to change extensions of files to .h/.hpp/.hh and nothing works :\ 

TDK
Super User
April 30, 2021

> Is any possible to setting header files to include C++ methods? When I include a library in .cpp file everything works fine, but the headers file really doesn't want to be treated as C++ files... I tried to change extensions of files to .h/.hpp/.hh and nothing works :\ 

Renaming the header file won't change the fact that "main.c" is not a C++ file and therefore cannot include C++ libraries.

"If you feel a post has answered your question, please click ""Accept as Solution""."
hs2
Visitor II
April 30, 2021

You mean C++ <cstring> or C <string.h> ? There is no standard <string> header file AFAIK.

My bad. There is a <string> - sorry !

TDK
TDKBest answer
Super User
April 30, 2021

<string> is for std::string objects. <cstring> and <string.h> are for strlen() and similar functions.

http://www.cplusplus.com/reference/string/string/

"If you feel a post has answered your question, please click ""Accept as Solution""."
hs2
Visitor II
April 30, 2021

Thanks TDK - fixed it already. It’s too late… ;)