cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to add an existing code (library) to a project.

shayan0373n
Associate II

I copy-pasted my library folder containing .cpp and .h files to the project root folder and added the include directories to both Include Paths in GCC and G++ compiler settings. But still when I'm trying to build the project, I get "undefined reference error" which indicates that my library source files are not getting built (or linked). I've changed the main.c to main.cpp as my library is in C++.

What am I doing wring?

2 REPLIES 2

With C++ you will need to be aware of name mangling, and use extern "C" when interfacing with C code, or mixing things up.

>>I get "undefined reference error" ..

To what specifically?

C++ is also going to have a lot more rigour when it comes to coding style, parameter types, and enums.

When mixing in C files, you might have to change file options to compile as C++, or manage the interfaces as indicated above.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thank you for your answer.

The fact is, I didn't have any problem to build the same project in other IDEs and I'm aware of name mingling and language linkage as well as other paradigms used in C++. I don't really think the problem is from my code.

>> To what specifically?

To every object usage in my main.cpp:

09:06:30 **** Build of configuration Debug for project milkmeter ****
make -j4 all 
arm-none-eabi-gcc -mcpu=cortex-m3 -g3 -c -x assembler-with-cpp --specs=nano.specs -mfloat-abi=soft -mthumb -o "Startup/startup_stm32f103retx.o" "../Startup/startup_stm32f103retx.s"
arm-none-eabi-g++ "../Src/main.cpp"
arm-none-eabi-gcc "../Src/stm32f1xx_hal_msp.c"
arm-none-eabi-gcc "../Src/stm32f1xx_it.c"
arm-none-eabi-gcc "../Src/syscalls.c"
arm-none-eabi-gcc "../Src/sysmem.c"
arm-none-eabi-gcc "../Src/system_stm32f1xx.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_adc.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_adc_ex.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_exti.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_gpio.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_rcc.c"
arm-none-eabi-gcc "../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_ll_utils.c"
arm-none-eabi-g++ -o "milkmeter.elf" @"objects.list"   -mcpu=cortex-m3 -T"C:\Users\M.J\STM32CubeIDE\workspace_1.0.2\milkmeter\STM32F103RETX_FLASH.ld" --specs=nosys.specs -Wl,-Map="milkmeter.map" -Wl,--gc-sections -static --specs=nano.specs -mfloat-abi=soft -mthumb -Wl,--start-group -lc -lm -lstdc++ -lsupc++ -Wl,--end-group
Src/main.o: In function `main':
C:/Users/M.J/STM32CubeIDE/workspace_1.0.2/milkmeter/Debug/../Src/main.cpp:100: undefined reference to `glcd::Init(glcd_device_mode)'
C:/Users/M.J/STM32CubeIDE/workspace_1.0.2/milkmeter/Debug/../Src/main.cpp:101: undefined reference to `gText::SelectFont(unsigned char const*, unsigned char, unsigned char (*)(unsigned char const*))'
C:/Users/M.J/STM32CubeIDE/workspace_1.0.2/milkmeter/Debug/../Src/main.cpp:102: undefined reference to `gText::Printf(char const*, ...)'
C:/Users/M.J/STM32CubeIDE/workspace_1.0.2/milkmeter/Debug/../Src/main.cpp:107: undefined reference to `gText::CursorTo(unsigned char, unsigned char)'
C:/Users/M.J/STM32CubeIDE/workspace_1.0.2/milkmeter/Debug/../Src/main.cpp:108: undefined reference to `gText::Printf(char const*, ...)'
C:/Users/M.J/STM32CubeIDE/workspace_1.0.2/milkmeter/Debug/../Src/main.cpp:107: undefined reference to `GLCD'
C:/Users/M.J/STM32CubeIDE/workspace_1.0.2/milkmeter/Debug/../Src/main.cpp:107: undefined reference to `ReadPgmData(unsigned char const*)'
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:56: milkmeter.elf] Error 1
"make -j4 all" terminated with exit code 2. Build might be incomplete.
 
09:06:37 Build Failed. 8 errors, 0 warnings. (took 6s.995ms)
 

My build log (which I removed some redundant build options) clearly shows that none of my library source files are getting built. Can it be that my library source files are .cpp?