cancel
Showing results for 
Search instead for 
Did you mean: 

How to add .cpp and .h files to the project of stm32cubeide?

OMaka.1
Associate II

1) I added to folder Core/Inc file my_lib.h

2) Added to folder Core/Src file my_lib.cpp

3) I added #include "my_lib.cpp" to main.cpp

In tmy_lib.cpp I have only one class for testing. This class works with PWM.

When I compile project I get errors that such names are not found:

TIM_Channel_1, htim, uint32_t, so on...

So, it seems that this my_lib.h is compiled first and before default libraries goted by CubeMX. How to solve this problem and to compile added file at the end of the project?

1 ACCEPTED SOLUTION

Accepted Solutions
OMaka.1
Associate II

OK, problem was solved.

I was wrong in syntax of classes in additional files. Now everything works fine.

View solution in original post

4 REPLIES 4
TDK
Guru

Don't #include source files from other source files. That's now how C or C++ is intended to work.

At the beginning of files that use HAL, #include "stm32f4xx_hal.h" or similar. Don't #include any other HAL files.

If you feel a post has answered your question, please click "Accept as Solution".
OMaka.1
Associate II

Thank you for your answer, it's helped a little. But may be I'm doint something wrong.

I got error "undefined reference to class:

c:\st\stm32cubeide_1.7.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_2.0.0.202105311346\tools\arm-none-eabi\bin\ld.exe: E:/STM/les_4_4_PWM_output/Debug/../Core/Src/main.cpp:177: undefined reference to `PWM::PWM()'
c:\st\stm32cubeide_1.7.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_2.0.0.202105311346\tools\arm-none-eabi\bin\ld.exe: E:/STM/les_4_4_PWM_output/Debug/../Core/Src/main.cpp:177: undefined reference to `PWM::PWM()'
c:\st\stm32cubeide_1.7.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_2.0.0.202105311346\tools\arm-none-eabi\bin\ld.exe: E:/STM/les_4_4_PWM_output/Debug/../Core/Src/main.cpp:177: undefined reference to `PWM::PWM()'
c:\st\stm32cubeide_1.7.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_2.0.0.202105311346\tools\arm-none-eabi\bin\ld.exe: E:/STM/les_4_4_PWM_output/Debug/../Core/Src/main.cpp:193: undefined reference to `PWM::SoftBlink(TIM_HandleTypeDef*, unsigned long, int, unsigned long)'
c:\st\stm32cubeide_1.7.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_2.0.0.202105311346\tools\arm-none-eabi\bin\ld.exe: E:/STM/les_4_4_PWM_output/Debug/../Core/Src/main.cpp:194: undefined reference to `PWM::SoftBlink(TIM_HandleTypeDef*, unsigned long, int, unsigned long)'
c:\st\stm32cubeide_1.7.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_2.0.0.202105311346\tools\arm-none-eabi\bin\ld.exe: E:/STM/les_4_4_PWM_output/Debug/../Core/Src/main.cpp:195: undefined reference to `PWM::SoftBlink(TIM_HandleTypeDef*, unsigned long, int, unsigned long)'
c:\st\stm32cubeide_1.7.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.win32_2.0.0.202105311346\tools\arm-none-eabi\bin\ld.exe: E:/STM/les_4_4_PWM_output/Debug/../Core/Src/main.cpp:196: undefined reference to `PWM::SoftBlink(TIM_HandleTypeDef*, unsigned long, int, unsigned long)'
collect2.exe: error: ld returned 1 exit status

I have added such .h file Core/Inc/mypwm.h

#ifndef INC_MYPWM_H_
#define INC_MYPWM_H_
#include "stm32f3xx_hal.h"
 
class PWM {
	protected:
		int32_t ch_dir; 
	private:
		uint32_t CH_DC; 
	public:
		PWM(); 
		void SoftBlink(TIM_HandleTypeDef *htim, uint32_t Channel, int step, uint32_t max_ARR);
 
}; // end class
#endif /* INC_MYPWM_H_ */

and Core/Src/mypwm.cpp is:

#include "stm32f3xx_hal.h"
#include "assert.h"
 
class PWM {
	protected:
		int32_t ch_dir; // =1 - increase pwm, -1 - decreese pwm
	private:
		uint32_t CH_DC; // max Auto Reload Register (ARR)
	public:
		PWM() // кон�?труктор за умовчуванн�?м
		{
			ch_dir=1;
			CH_DC=0;
		}
		void SoftBlink(TIM_HandleTypeDef *htim, uint32_t Channel, int step, uint32_t max_ARR){
			   while ((CH_DC < max_ARR) & (ch_dir >=0)) {
			    	switch (Channel){
			    		case TIM_CHANNEL_1 : {
			    			htim->Instance->CCR1 = CH_DC;
			    			break;
			    		}
			    		case TIM_CHANNEL_2 : {
			    			htim->Instance->CCR2 = CH_DC;
			    			break;
			    		}
			    		case TIM_CHANNEL_3 : {
			    			htim->Instance->CCR3 = CH_DC;
			    			break;
			    		}
			    		case TIM_CHANNEL_4 : {
			    			htim->Instance->CCR4 = CH_DC;
			    			break;
			    		}
			    		default :
			    			assert(0);
			    	} // end_switch
			    		CH_DC += step;
				    	HAL_Delay(1);
			    } // end_while
 
			    while ((CH_DC > 0) & (ch_dir <0)) {
			    	switch (Channel){
			    		case TIM_CHANNEL_1 : {
			    			htim->Instance->CCR1 = CH_DC;
			    			break;
			    		}
			    		case TIM_CHANNEL_2 : {
			    			htim->Instance->CCR2 = CH_DC;
			    			break;
			    		}
			    		case TIM_CHANNEL_3 : {
			    			htim->Instance->CCR3 = CH_DC;
			    			break;
			    		}
			    		case TIM_CHANNEL_4 : {
			    			htim->Instance->CCR4 = CH_DC;
			    			break;
			    		}
			    	} // end_switch
			    	CH_DC -=step;
			    	HAL_Delay(1);
			    } // end_while
			  if (ch_dir>=0) {
				  ch_dir =-1;
			  }
			  else {
				  ch_dir=1;
			  }
		  } //end SoftBlink
}; // end class

I don't anderstand what I'm doing wrong.

In the main.cpp I make

#include "mypwm.h"

OMaka.1
Associate II

OK, problem was solved.

I was wrong in syntax of classes in additional files. Now everything works fine.

TDK
Guru

Make sure mypwm.cpp is being compiled. Look in the build log.

Try to right click the project and clean/refresh.

If you feel a post has answered your question, please click "Accept as Solution".