How can I change c standard to c99 or newer

I’m trying to learn to develop hardware msp430g2553 with vscode + platformio. * However, it is shown during compilation that the c99 standard is not supported.
I tried code like:

it told me like below:

#include "msp430.h"

int main()
{
    WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
    P2SEL &= ~(BIT2 | BIT5);
    P2SEL2 &= ~(BIT2 | BIT5);
    P2OUT |= BIT2 | BIT5;
    P2DIR |= BIT2 | BIT5;
    unsigned int L = 0, M = 0;
    while (1)
    {
        L++;
        if (1 > 0x200)
            L = 0;
        M = 0x200 - L;
        P2OUT &= ~(BIT2 | BIT5);
        for (int i = 0; i < L; i++)
        {
            P2OUT |= (BIT2 | BIT5);
        }
        for (int i = 0; i < M; i++)
            ;
    }

    return 0;
}
src\main.c: In function 'main':
src\main.c:18:9: error: 'for' loop initial declarations are only allowed in C99 mode
src\main.c:18:9: note: use option -std=c99 or -std=gnu99 to compile your code
src\main.c:22:18: error: redefinition of 'i'
src\main.c:18:18: note: previous definition of 'i' was here
src\main.c:22:9: error: 'for' loop initial declarations are only allowed in C99 mode
  • I do know that this problem can be avoided by changing the code,but I * But I still want to get it to support the updated c standard.

I have tried writen ‘build_flags = -std=c99’ in platform.ini ,but it not works.

I try to find blogs about this , nothing was found ,maybe i have not used right key words

My English is not very good, thank you for reading this.

Hoping discussing!

That’s one part of it, but first you gotta find out what the current -std value is so that you that you can build_unflags = -std=... it.

Do so by using the project task Advanced → Verbose Build.

I have added code like

build_unflags = 
    -std=c89
    -std=gnu89
    -std=c95
    -std=gnu95

but it not work
I build it ,and it tells me

PLATFORM: TI MSP430 (2.4.0) > TI LaunchPad MSP-EXP430G2553LP
HARDWARE: MSP430G2553 16MHz, 512B RAM, 16KB Flash
DEBUG: Current (mspdebug) On-board (mspdebug)
PACKAGES:
 - framework-energiamsp430 @ 1.10810.210219 (1.8.10)        
 - toolchain-timsp430 @ 1.40603.210219 (4.6.3)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 18 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
msp430-gcc -o .pio\build\lpmsp430g2553\src\main.c.o -c -mmcu=msp430g2553 -Os -ffunction-sections -fdata-sections -DF_CPU=16000000L -DPLATFORMIO=60111 -DENERGIA_ARCH_MSP430 -DENERGIA_MSP_EXP430G2553LP -DARDUINO=10807 -DENERGIA=10810 -Iinclude -Isrc -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\variants\MSP-EXP430G2553LP -IC:\Users\yixing\.platformio\packages\toolchain-timsp430\msp430\include src\main.c
src\main.c: In function 'main':
src\main.c:18:9: error: 'for' loop initial declarations are only allowed in C99 mode
src\main.c:18:9: note: use option -std=c99 or -std=gnu99 to compile your code       
src\main.c:22:18: error: redefinition of 'i'
src\main.c:18:18: note: previous definition of 'i' was here
src\main.c:22:9: error: 'for' loop initial declarations are only allowed in C99 mode
msp430-g++ -o .pio\build\lpmsp430g2553\FrameworkEnergia\new.cpp.o -c -fno-exceptions -fno-threadsafe-statics -mmcu=msp430g2553 -Os -ffunction-sections -fdata-sections -DF_CPU=16000000L -DPLATFORMIO=60111 -DENERGIA_ARCH_MSP430 -DENERGIA_MSP_EXP430G2553LP -DARDUINO=10807 -DENERGIA=10810 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\variants\MSP-EXP430G2553LP -IC:\Users\yixing\.platformio\packages\toolchain-timsp430\msp430\include C:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430\new.cpp
msp430-gcc -o .pio\build\lpmsp430g2553\FrameworkEnergia\random.c.o -c -mmcu=msp430g2553 -Os -ffunction-sections -fdata-sections -DF_CPU=16000000L -DPLATFORMIO=60111 -DENERGIA_ARCH_MSP430 -DENERGIA_MSP_EXP430G2553LP -DARDUINO=10807 -DENERGIA=10810 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\variants\MSP-EXP430G2553LP -IC:\Users\yixing\.platformio\packages\toolchain-timsp430\msp430\include C:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430\random.c
msp430-gcc -o .pio\build\lpmsp430g2553\FrameworkEnergia\usci_isr_handler.c.o -c -mmcu=msp430g2553 -Os -ffunction-sections -fdata-sections -DF_CPU=16000000L -DPLATFORMIO=60111 -DENERGIA_ARCH_MSP430 -DENERGIA_MSP_EXP430G2553LP -DARDUINO=10807 -DENERGIA=10810 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\variants\MSP-EXP430G2553LP -IC:\Users\yixing\.platformio\packages\toolchain-timsp430\msp430\include C:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430\usci_isr_handler.c
msp430-gcc -o .pio\build\lpmsp430g2553\FrameworkEnergia\wiring.c.o -c -mmcu=msp430g2553 -Os -ffunction-sections -fdata-sections -DF_CPU=16000000L -DPLATFORMIO=60111 -DENERGIA_ARCH_MSP430 -DENERGIA_MSP_EXP430G2553LP -DARDUINO=10807 -DENERGIA=10810 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\variants\MSP-EXP430G2553LP -IC:\Users\yixing\.platformio\packages\toolchain-timsp430\msp430\include C:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430\wiring.c
msp430-gcc -o .pio\build\lpmsp430g2553\FrameworkEnergia\wiring_analog.c.o -c -mmcu=msp430g2553 -Os -ffunction-sections -fdata-sections -DF_CPU=16000000L -DPLATFORMIO=60111 -DENERGIA_ARCH_MSP430 -DENERGIA_MSP_EXP430G2553LP -DARDUINO=10807 -DENERGIA=10810 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\variants\MSP-EXP430G2553LP -IC:\Users\yixing\.platformio\packages\toolchain-timsp430\msp430\include C:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430\wiring_analog.c
msp430-gcc -o .pio\build\lpmsp430g2553\FrameworkEnergia\wiring_digital.c.o -c -mmcu=msp430g2553 -Os -ffunction-sections -fdata-sections -DF_CPU=16000000L -DPLATFORMIO=60111 -DENERGIA_ARCH_MSP430 -DENERGIA_MSP_E*** [.pio\build\lpmsp430g2553\src\main.c.o] Error 1
XP430G2553LP -DARDUINO=10807 -DENERGIA=10810 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\variants\MSP-EXP430G2553LP -IC:\Users\yixing\.platformio\packages\toolchain-timsp430\msp430\include C:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430\wiring_digital.c
msp430-gcc -o .pio\build\lpmsp430g2553\FrameworkEnergia\wiring_pulse.c.o -c -mmcu=msp430g2553 -Os -ffunction-sections -fdata-sections -DF_CPU=16000000L -DPLATFORMIO=60111 -DENERGIA_ARCH_MSP430 -DENERGIA_MSP_EXP430G2553LP -DARDUINO=10807 -DENERGIA=10810 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\variants\MSP-EXP430G2553LP -IC:\Users\yixing\.platformio\packages\toolchain-timsp430\msp430\include C:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430\wiring_pulse.c
msp430-gcc -o .pio\build\lpmsp430g2553\FrameworkEnergia\wiring_shift.c.o -c -mmcu=msp430g2553 -Os -ffunction-sections -fdata-sections -DF_CPU=16000000L -DPLATFORMIO=60111 -DENERGIA_ARCH_MSP430 -DENERGIA_MSP_EXP430G2553LP -DARDUINO=10807 -DENERGIA=10810 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430 -IC:\Users\yixing\.platformio\packages\framework-energiamsp430\variants\MSP-EXP430G2553LP -IC:\Users\yixing\.platformio\packages\toolchain-timsp430\msp430\include C:\Users\yixing\.platformio\packages\framework-energiamsp430\cores\msp430\wiring_shift.c
  • I noticed that my build chain didn’t specify anything standards-related. I don’t quite understand.

Energia is Arduino. In your code, you try to define a main() function, but the Arduino framework already has that… so that is fundamentally wrong usage.

In any case though,

[env:lpmsp430g2553]
platform = timsp430
board = lpmsp430g2553
framework = arduino
build_flags = -std=gnu99

works fine in this case.

To avoid compiling in the Arduino code alltogether, just remove the framework = ... line in the platformio.ini, then it’ll be bare-metal. This still compiles the file normally and efficiently.

[env:lpmsp430g2553]
platform = timsp430
board = lpmsp430g2553
build_flags = -std=gnu99

OHHHHHHHHHH it works!
Thankyou,Thankyou,Thankyou.
just now,i try change the file neme to 'main.cpp’from ‘main.c’,and it also works (i am not understand again).

The C++ language allows these variable specifications with whatever language version, so this is never illegal code.

Thank you for your patience!!!