cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeIDE compiles main() from the Core folder instead of my user folder.

Tamas Novak
Associate III

STM32CubeIDE project skeleton generated, I stuffed main.c (between USER CODE BEGIN/ENDs) with my code. Project is built successfully. When I start debug, it downloads into my custom board through STLink, then stops at an initial breakpoint at HAL_Init(). The only problem, it is not in my main.c, but it loads original empty Project\Core\Src\main.c instead of my Project\Src\main.c. When debug started, only Project/Scr/main.c is opened in editor. When debug stops at HAL_Init() breakpoint, it loads Project/Core/Scr/main.c to a different editor tab. It seems the main() is compiled and linked from the Core folder instead of my user folder. Where to find the setting to avoid this?

6 REPLIES 6
Pavel A.
Evangelist III

STM32CubeIDE is Eclipse CDT. Eclipse "managed builder" by default compiles all C files it can find in the project tree.

So if there are two "main" files, you must delete the wrong one, or exclude it from build. Else, Eclipse will randomly select one for you.

-- pa

Tamas Novak
Associate III

That much is clear. It seems old separate CubeMX generated the user files into Project/Scr and Project/Inc, and new CubeIDE creates Project/Core/Src and Project/Core/Inc paths. If you migrate an old project (has been previously code-generated), then you will have two sets of files to be user-edited. So take care, move the valuable stuff to Core folder, than remove \Src and \Inc folders.

The question really is: how CubeIDE allows me to have two main(). It simply "picks one" without any error/warning messages?!! If I have two definitions of functions with the same name, I got "Multiple definition of ***" error. Compiler "sees" only a single source file, but linker will discover the two same-name functions in two modules.

Pavel A.
Evangelist III

> The question really is: how CubeIDE allows me to have two main(). It simply "picks one" without any error/warning messages?!! 

Maybe the other main.c is already compiled, so the make system does not compile it again. But links both objects.

-- pa

Tamas Novak
Associate III

@Pavel A.​  While struggling I have made Clean, so old objects were deleted. The question for me: how CubeIDE generates makefile from the "Project Explorer" list.

My project Explorer lists Core/Src and /Src, both with a main.c. Still generated makefile contains only /Core/Src, so only 1 main.c is compiled. As /Src/main.c resides on the Project Explorer list, I can open it in editor, and it will be saved when project saved, so it seems funtional. Still, auto-regenerated makefile misses Src/main.c, so I don't have a warning when compile+link. When I tried to remove Core/Src from the Project, I got a lot of "*** missing error" when Make.

So how exactly makefile is generated. There are some stuff on Project Explorer list (*.ioc, *.pdf), which are not put on the makelist, so there must be some kinda filtering mechanism.

Pavel A.
Evangelist III

> The question for me: how CubeIDE generates makefile from the "Project Explorer" list

It inherited this from Eclipse CDT.

There's more than a single makefile. It should create a "object" tree that mirrors source tree, with child makefiles named subdir.mk in each subdirectory.

After you add or remove source files, do refresh in the eclipse (press F5).

-- pa

Tamas Novak
Associate III

A new chapter of the same story:

I started a new project from scratch. New folder and .IOC file created by CubeIDE, then C skeleton generated. Strange that two main.c is generated. In my previous comment I thought "Project/Src/main.c" is left there from an old MX run, and new "Project/Core/Src/main.c" is freshly generated. But it happened that both main.c is generated by CubeIDE. The two files are not the same.

Core/Src contains variables for initializing peripheral modules (line 44-53)

/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
 
/* Includes ------------------------------------------------------------------*/
#include "main.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
 
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
 
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
 
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc;
 
CRC_HandleTypeDef hcrc;
 
IWDG_HandleTypeDef hiwdg;
 
TIM_HandleTypeDef htim14;
 
UART_HandleTypeDef huart1;
DMA_HandleTypeDef hdma_usart1_tx;
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_ADC_Init(void);
static void MX_CRC_Init(void);
static void MX_IWDG_Init(void);
static void MX_TIM14_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */

ulnike /Src which is really empty:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
 
/* Includes ------------------------------------------------------------------*/
#include "main.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
 
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
 
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
 
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

Can anyone explain to me what is it good for? Auto-generated make file only contains Core/Src, so if you write your own sources into this, everything is fine. But when a newbie is looking for the folders, Core will be abandoned (some ARM lib maybe?) and he/she is shooting for Src.

Very deceptive...