cancel
Showing results for 
Search instead for 
Did you mean: 

cubeMX generated project, compiling main.cpp using Makefile

JuniorSTM
Associate II

Hi, I try to change compiling code method to Makefile. I'm using generated from cubeMX makefile with few changes. Everything works file when I compiling main.c file but, when I change file to main.cpp compiler print errors. I tried to fix this but, my knowledge isn't enough :\

Added lines are in #VV/#^^ comments.

Now I have this error code:

....
arm-none-eabi-gcc -x assembler-with-cpp -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32L152xE -ICore/Inc -IDrivers/STM32L1xx_HAL_Driver/Inc -IDrivers/STM32L1xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32L1xx/Include -IDrivers/CMSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/startup_stm32l152xe.d" startup_stm32l152xe.s -o build/startup_stm32l152xe.o
arm-none-eabi-gcc -c -mcpu=cortex-m3 -mthumb   -DUSE_HAL_DRIVER -DSTM32L152xE -ICore/Inc -IDrivers/STM32L1xx_HAL_Driver/Inc -IDrivers/STM32L1xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32L1xx/Include -IDrivers/CMSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/main.d" -Wa,-a,-ad,-alms=build/main.lst Core/Src/main.cpp -o build/main.o
arm-none-eabi-gcc build/gpio.o build/usart.o build/stm32l1xx_it.o build/stm32l1xx_hal_msp.o build/stm32l1xx_hal_tim.o build/stm32l1xx_hal_tim_ex.o build/stm32l1xx_hal_uart.o build/stm32l1xx_hal.o build/stm32l1xx_hal_rcc.o build/stm32l1xx_hal_rcc_ex.o build/stm32l1xx_hal_flash.o build/stm32l1xx_hal_flash_ex.o build/stm32l1xx_hal_flash_ramfunc.o build/stm32l1xx_hal_gpio.o build/stm32l1xx_hal_dma.o build/stm32l1xx_hal_pwr.o build/stm32l1xx_hal_pwr_ex.o build/stm32l1xx_hal_cortex.o build/stm32l1xx_hal_exti.o build/system_stm32l1xx.o build/startup_stm32l152xe.o build/main.o -mcpu=cortex-m3 -mthumb   -specs=nano.specs -TSTM32L152RETx_FLASH.ld  -lc -lm -lnosys  -Wl,-Map=build/skytrap.map,--cref -Wl,--gc-sections -o build/skytrap.elf
c:/dev/tools/gcc-arm-none-eabi-10-2020-q4-major/bin/../lib/gcc/arm-none-eabi/10.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/dev/tools/gcc-arm-none-eabi-10-2020-q4-major/bin/../lib/gcc/arm-none-eabi/10.2.1/../../../../arm-none-eabi/lib/thumb/v7-m/nofp\libc_nano.a(lib_a-signalr.o): in function `_kill_r':
signalr.c:(.text._kill_r+0xe): undefined reference to `_kill'
c:/dev/tools/gcc-arm-none-eabi-10-2020-q4-major/bin/../lib/gcc/arm-none-eabi/10.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/dev/tools/gcc-arm-none-eabi-10-2020-q4-major/bin/../lib/gcc/arm-none-eabi/10.2.1/../../../../arm-none-eabi/lib/thumb/v7-m/nofp\libc_nano.a(lib_a-signalr.o): in function `_getpid_r':
signalr.c:(.text._getpid_r+0x0): undefined reference to `_getpid'
collect2.exe: error: ld returned 1 exit status
make: *** [build/skytrap.elf] Błąd 1

Makefile:

##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [3.13.0-B3] date: [Wed May 05 12:06:14 GMT+02:00 2021] 
##########################################################################################################################
 
# ------------------------------------------------
# Generic Makefile (based on gcc)
#
# ChangeLog :
#	2017-02-10 - Several enhancements + project update mode
#   2015-07-22 - first version
# ------------------------------------------------
 
######################################
# target
######################################
TARGET = skytrap
 
 
######################################
# building variables
######################################
# debug build?
DEBUG = 1
# optimization
OPT = -Og
 
 
#######################################
# paths
#######################################
# Build path
BUILD_DIR = build
 
######################################
# source
######################################
# C sources
#VV there remove main.c
C_SOURCES =  \
Core/Src/gpio.c \
Core/Src/usart.c \
Core/Src/stm32l1xx_it.c \
Core/Src/stm32l1xx_hal_msp.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_tim.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_tim_ex.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_uart.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_rcc.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_rcc_ex.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_flash.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_flash_ex.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_flash_ramfunc.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_gpio.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_dma.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_pwr.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_pwr_ex.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_cortex.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_exti.c \
Core/Src/system_stm32l1xx.c
 
# ASM sources
ASM_SOURCES =  \
startup_stm32l152xe.s
 
#VV
CXXSRC= \
Core/Src/main.cpp
#^^
 
 
 
#######################################
# binaries
#######################################
PREFIX = arm-none-eabi-
# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=***)
# either it can be added to the PATH environment variable.
ifdef GCC_PATH
CC = $(GCC_PATH)/$(PREFIX)gcc
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
else
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
 
#######################################
# CFLAGS
#######################################
# cpu
CPU = -mcpu=cortex-m3
 
# fpu
# NONE for Cortex-M0/M0+/M3
 
# float-abi
 
 
# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
 
# macros for gcc
# AS defines
AS_DEFS = 
 
# C defines
C_DEFS =  \
-DUSE_HAL_DRIVER \
-DSTM32L152xE
 
 
# AS includes
AS_INCLUDES = 
 
# C includes
C_INCLUDES =  \
-ICore/Inc \
-IDrivers/STM32L1xx_HAL_Driver/Inc \
-IDrivers/STM32L1xx_HAL_Driver/Inc/Legacy \
-IDrivers/CMSIS/Device/ST/STM32L1xx/Include \
-IDrivers/CMSIS/Include
 
 
# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
 
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
 
ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif
 
 
# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
 
 
#######################################
# LDFLAGS
#######################################
# link script
LDSCRIPT = STM32L152RETx_FLASH.ld
 
# libraries
LIBS = -lc -lm -lnosys 
LIBDIR = 
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
 
# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
 
 
#######################################
# build the application
#######################################
# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))
#VV
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(CXXSRC:.cpp=.o)))
vpath %.cpp $(sort $(dir $(CXXSRC)))
#^^
 
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(

I hope someone can resolve this issue =) 

I will be grateful for any help.

@Edit:

I'm not sure if I need to use G++ compiler for .cpp files?

GCC ver: gcc-arm-none-eabi-10-2020-q4-major

openocd ver: openocd-20210503

4 REPLIES 4
TDK
Guru

> I'm not sure if I need to use G++ compiler for .cpp files?

Yes, of course you do. G++ for C++ and gcc for C.

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

You need to add the g++ compiler:

PREFIX = arm-none-eabi-

ifdef GCC_PATH

CC = $(GCC_PATH)/$(PREFIX)gcc

CPP = $(GCC_PATH)/$(PREFIX)g++

AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp

CP = $(GCC_PATH)/$(PREFIX)objcopy

SZ = $(GCC_PATH)/$(PREFIX)size

else

CC = $(PREFIX)gcc

CPP = $(PREFIX)g++

AS = $(PREFIX)gcc -x assembler-with-cpp

CP = $(PREFIX)objcopy

SZ = $(PREFIX)size

endif

Also you need to add sources, includes, and cppflags:

CPP_SOURCES = \

Core/Src/main.cpp \

CPP_INCLUDES = \

Core/Inc \

CPPFLAGS = $(CPP_INCLUDES) -std=c++17 -Wno-register

And add this in the build application section:

OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(CPP_SOURCES:.cpp=.o)))

vpath %.cpp $(sort $(dir $(CPP_SOURCES)))

$(BUILD_DIR)/%.o: %.cpp Makefile | $(BUILD_DIR)

$(CPP) -c $(CFLAGS) $(CPPFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.cpp=.lst)) $< -o $@

JuniorSTM
Associate II

I did few updates, but still do not work and have the same error. For example, I changed the CXXFLAGS. When I change  CXXF. on "#CXXFLAGS = $(C_INCLUDES) -std=c++17 -Wno-register #ERROR 1" I had an error like below [ERROR 1]

...................  
139 |   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
      |                                     ^~~~~~~~~~~~~~~ 
Core/Src/main.cpp:140:21: error: 'struct RCC_ClkInitTypeDef' has no member named 'APB1CLKDivider'
  140 |   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; 
      |                     ^~~~~~~~~~~~~~
In file included from Core/Inc/stm32l1xx_hal_conf.h:197,    
                 from Drivers/STM32L1xx_HAL_Driver/Inc/stm32l1xx_hal.h:30,
                 from Core/Inc/main.h:31,
                 from Core/Src/main.cpp:21:
Drivers/STM32L1xx_HAL_Driver/Inc/stm32l1xx_hal_rcc.h:477:42: error: 'RCC_CFGR_PPRE1_DIV1' was not declared in this scope
  477 | #define RCC_HCLK_DIV1                    RCC_CFGR_PPRE1_DIV1  /*!< HCLK not divided */
      |                                          ^~~~~~~~~~~~~~~~~~~
Core/Src/main.cpp:140:38: note: in expansion of macro 'RCC_HCLK_DIV1'
  140 |   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; 
      |                                      ^~~~~~~~~~~~~  
Core/Src/main.cpp:141:21: error: 'struct RCC_ClkInitTypeDef' has no member named 'APB2CLKDivider'
  141 |   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 
      |                     ^~~~~~~~~~~~~~
In file included from Core/Inc/stm32l1xx_hal_conf.h:233,    
                 from Drivers/STM32L1xx_HAL_Driver/Inc/stm32l1xx_hal.h:30,
                 from Core/Inc/main.h:31,
                 from Core/Src/main.cpp:21:
Drivers/STM32L1xx_HAL_Driver/Inc/stm32l1xx_hal_flash.h:144:36: error: 'FLASH_ACR_LATENCY' was not declared in this scope; did you mean 'FLASH_LATENCY_1'?
  144 | #define FLASH_LATENCY_1            FLASH_ACR_LATENCY         /*!< FLASH One Latency cycle */
      |                                    ^~~~~~~~~~~~~~~~~
Core/Src/main.cpp:143:47: note: in expansion of macro 'FLASH_LATENCY_1'
  143 |   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
      |                                               ^~~~~~~~~~~~~~~
Core/Src/main.cpp: In function 'void Error_Handler()':      
Core/Src/main.cpp:161:3: error: '__disable_irq' was not declared in this scope
  161 |   __disable_irq();
      |   ^~~~~~~~~~~~~
make: *** [build/main.o] Błąd 1

Also updated makefile

##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [3.13.0-B3] date: [Wed May 05 12:06:14 GMT+02:00 2021] 
##########################################################################################################################
 
# ------------------------------------------------
# Generic Makefile (based on gcc)
#
# ChangeLog :
#	2017-02-10 - Several enhancements + project update mode
#   2015-07-22 - first version
# ------------------------------------------------
 
######################################
# target
######################################
TARGET = skytrap
 
 
######################################
# building variables
######################################
# debug build?
DEBUG = 1
# optimization
OPT = -Og
 
 
#######################################
# paths
#######################################
# Build path
BUILD_DIR = build
 
######################################
# source
######################################
# C sources
#VV there remove main.c
C_SOURCES =  \
Core/Src/gpio.c \
Core/Src/usart.c \
Core/Src/stm32l1xx_it.c \
Core/Src/stm32l1xx_hal_msp.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_tim.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_tim_ex.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_uart.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_rcc.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_rcc_ex.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_flash.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_flash_ex.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_flash_ramfunc.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_gpio.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_dma.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_pwr.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_pwr_ex.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_cortex.c \
Drivers/STM32L1xx_HAL_Driver/Src/stm32l1xx_hal_exti.c \
Core/Src/system_stm32l1xx.c
 
# ASM sources
ASM_SOURCES =  \
startup_stm32l152xe.s
 
#VV
CXX_SOURCES= \
Core/Src/main.cpp
#^^
 
 
 
#######################################
# binaries
#######################################
PREFIX = arm-none-eabi-
# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=***)
# either it can be added to the PATH environment variable.
ifdef GCC_PATH
CC = $(GCC_PATH)/$(PREFIX)gcc
#VV
CXX = $(GCC_PATH)/$(PREFIX)g++
#^^
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
else
CC = $(PREFIX)gcc
#VV
CXX = $(PREFIX)g++
#^^
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
 
#######################################
# CFLAGS
#######################################
# cpu
CPU = -mcpu=cortex-m3
 
# fpu
# NONE for Cortex-M0/M0+/M3
 
# float-abi
 
 
# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
 
# macros for gcc
# AS defines
AS_DEFS = 
 
# C defines
C_DEFS =  \
-DUSE_HAL_DRIVER \
-DSTM32L152xE
 
 
# AS includes
AS_INCLUDES = 
 
# C includes
C_INCLUDES =  \
-ICore/Inc \
-IDrivers/STM32L1xx_HAL_Driver/Inc \
-IDrivers/STM32L1xx_HAL_Driver/Inc/Legacy \
-IDrivers/CMSIS/Device/ST/STM32L1xx/Include \
-IDrivers/CMSIS/Include
 
CXX_INCLUDES = \
-ICore/Inc 
 
# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
 
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -std=c99
#CXXFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -std=c++17 -Wno-register #ERROR 0
CXXFLAGS = $(C_INCLUDES) -std=c++17 -Wno-register		#ERROR 1
 
ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif
 
 
# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
 
 
#######################################
# LDFLAGS
#######################################
# link script
LDSCRIPT = STM32L152RETx_FLASH.ld
 
# libraries
LIBS = -lc -lm -lnosys 
LIBDIR = 
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
 
# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
 
 
#######################################
# build the application
#######################################
# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))
#VV
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(CXX_SOURCES:.cpp=.o)))
vpath %.cpp $(sort $(dir $(CXX_SOURCES)))
#^^
 
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(

 

suads
Associate III

Hi,

I m not sure about your errors!!

But i think it could be including c files in a cpp project. To use c files in a cpp file. Make sure that the C headers are inside extern "C" { ... } block.

So the C files you include in a cpp project need to be inside a  extern "C" { ... } block.

So for example if you include main.h in main.cpp, the code from main.h need to be inside this block.

Example main.h

#ifndef __MAIN_H

#define __MAIN_H

#ifdef __cplusplus

#ifdef __cplusplus

extern "C" {

#endif

//main contents of header

//here goes the code from main.h

#ifdef __cplusplus

}

#endif

#endif /* MAIN_H*/