Problem with linking .cpp files

Hey, I’m quite new to C++ but never had an issue with including .h and .c++ files before…
What am I doing wrong here?

Daisy.cpp

#include "my_test.h"

MyTest mt;

int main(void)
{

	mt.Init();

	while(1) {}
}

my_test.h

#pragma once

class MyTest
{
private:
    /* data */
public:
    MyTest(/* args */){};
    ~MyTest(){};
    
    void Init();
    float test;
};

my_test.cpp

#include "my_test.h"

void MyTest::Init(){
    test = 55.0f;
}


ouput:

rm -fR build
mkdir build
arm-none-eabi-gcc -c -mcpu=cortex-m7 -mthumb -mfpu=fpv5-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32H750xx -DHSE_VALUE=16000000  -DCORE_CM7 -DSTM32H750IB -DARM_MATH_CM7 -DUSE_FULL_LL_DRIVER -DDEBUG -include stm32h7xx.h -I../../../Programs/githubs/DaisyExamples/libDaisy/ -I../../../Programs/githubs/DaisyExamples/libDaisy//src/ -I../../../Programs/githubs/DaisyExamples/libDaisy//src/sys -I../../../Programs/githubs/DaisyExamples/libDaisy//src/usbd -I../../../Programs/githubs/DaisyExamples/libDaisy//src/usbh -I../../../Programs/githubs/DaisyExamples/libDaisy//Drivers/CMSIS/Include/ -I../../../Programs/githubs/DaisyExamples/libDaisy//Drivers/CMSIS/DSP/Include -I../../../Programs/githubs/DaisyExamples/libDaisy//Drivers/CMSIS/Device/ST/STM32H7xx/Include -I../../../Programs/githubs/DaisyExamples/libDaisy//Drivers/STM32H7xx_HAL_Driver/Inc/ -I../../../Programs/githubs/DaisyExamples/libDaisy//Middlewares/ST/STM32_USB_Device_Library/Core/Inc -I../../../Programs/githubs/DaisyExamples/libDaisy//Middlewares/ST/STM32_USB_Host_Library/Core/Inc -I../../../Programs/githubs/DaisyExamples/libDaisy//Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Inc -I../../../Programs/githubs/DaisyExamples/libDaisy//core/ -I../../../Programs/githubs/DaisyExamples/DaisySP//Source -I../../../Programs/githubs/DaisyExamples/libDaisy//Middlewares/Third_Party/FatFs/src -O2 -Wall -Wno-missing-attributes -fasm -fdata-sections -ffunction-sections -Wno-stringop-overflow -g -ggdb -MMD -MP -MF"build/startup_stm32h750xx.d" -std=gnu11 -Wa,-a,-ad,-alms=build/startup_stm32h750xx.lst ../../../Programs/githubs/DaisyExamples/libDaisy//core/startup_stm32h750xx.c -o build/startup_stm32h750xx.o
arm-none-eabi-g++  -c -mcpu=cortex-m7 -mthumb -mfpu=fpv5-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32H750xx -DHSE_VALUE=16000000  -DCORE_CM7 -DSTM32H750IB -DARM_MATH_CM7 -DUSE_FULL_LL_DRIVER -DDEBUG -include stm32h7xx.h -I../../../Programs/githubs/DaisyExamples/libDaisy/ -I../../../Programs/githubs/DaisyExamples/libDaisy//src/ -I../../../Programs/githubs/DaisyExamples/libDaisy//src/sys -I../../../Programs/githubs/DaisyExamples/libDaisy//src/usbd -I../../../Programs/githubs/DaisyExamples/libDaisy//src/usbh -I../../../Programs/githubs/DaisyExamples/libDaisy//Drivers/CMSIS/Include/ -I../../../Programs/githubs/DaisyExamples/libDaisy//Drivers/CMSIS/DSP/Include -I../../../Programs/githubs/DaisyExamples/libDaisy//Drivers/CMSIS/Device/ST/STM32H7xx/Include -I../../../Programs/githubs/DaisyExamples/libDaisy//Drivers/STM32H7xx_HAL_Driver/Inc/ -I../../../Programs/githubs/DaisyExamples/libDaisy//Middlewares/ST/STM32_USB_Device_Library/Core/Inc -I../../../Programs/githubs/DaisyExamples/libDaisy//Middlewares/ST/STM32_USB_Host_Library/Core/Inc -I../../../Programs/githubs/DaisyExamples/libDaisy//Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Inc -I../../../Programs/githubs/DaisyExamples/libDaisy//core/ -I../../../Programs/githubs/DaisyExamples/DaisySP//Source -I../../../Programs/githubs/DaisyExamples/libDaisy//Middlewares/Third_Party/FatFs/src -O2 -Wall -Wno-missing-attributes -fasm -fdata-sections -ffunction-sections -Wno-stringop-overflow -g -ggdb -MMD -MP -MF"build/Daisy.d" -fno-exceptions -fasm -finline -finline-functions-called-once -fshort-enums -fno-move-loop-invariants -fno-unwind-tables -fno-rtti -Wno-register -std=gnu++14 -Wa,-a,-ad,-alms=build/Daisy.lst Daisy.cpp -o build/Daisy.o
/tmp/cc0sdtyw.s: Assembler messages:
/tmp/cc0sdtyw.s: Warning: unable to rebuffer file: Daisy.cpp

/tmp/cc0sdtyw.s: Warning: unable to rebuffer file: Daisy.cpp

arm-none-eabi-g++  build/startup_stm32h750xx.o build/Daisy.o  -mcpu=cortex-m7 -mthumb -mfpu=fpv5-d16 -mfloat-abi=hard --specs=nano.specs --specs=nosys.specs -T../../../Programs/githubs/DaisyExamples/libDaisy//core/STM32H750IB_flash.lds -L../../../Programs/githubs/DaisyExamples/libDaisy//build -L ../../../Programs/githubs/DaisyExamples/DaisySP//build -ldaisy -lc -lm -lnosys -ldaisysp -Wl,-Map=build/Daisy.map,--cref -Wl,--gc-sections -Wl,--print-memory-usage -o build/Daisy.elf
/usr/bin/../lib/gcc/arm-none-eabi/11.2.1/../../../../arm-none-eabi/bin/ld: build/Daisy.o: in function `main':
/home/van/Projects/HEMAU/Daisy/Daisy.cpp:8: undefined reference to `_ZN6MyTest4InitEv'
Memory region         Used Size  Region Size  %age Used
           FLASH:        1272 B       128 KB      0.97%
         DTCMRAM:          0 GB       128 KB      0.00%
            SRAM:          36 B       512 KB      0.01%
          RAM_D2:          0 GB       288 KB      0.00%
          RAM_D3:          0 GB        64 KB      0.00%
         ITCMRAM:          0 GB        64 KB      0.00%
           SDRAM:          0 GB        64 MB      0.00%
       QSPIFLASH:          0 GB         8 MB      0.00%
collect2: error: ld returned 1 exit status
make: *** [../../../Programs/githubs/DaisyExamples/libDaisy//core/Makefile:277: build/Daisy.elf] Error 1

 *  The terminal process "/bin/bash '-c', 'make clean;DEBUG=1 make'" terminated with exit code: 2. 
 *  Terminal will be reused by tasks, press any key to close it. 

Any Idea?

1 Like

oh wow… is this correct? I’ll have to include every .cpp file in the makefile manually?

makefile

# Sources
CPP_SOURCES += Daisy.cpp
CPP_SOURCES += my_test.cpp
1 Like

Yes, that’s the pattern used by most of the examples. It’s not the most elegant thing ever, but it does help make it clear which source files are being compiled.

As an alternative, you can use the wildcard function in Make, e.g.:

CPP_SOURCES = $(wildcard *.cpp)