PlatformIO Community

A ridiculous problem between c and cpp file?

OK, my problem is very diffcult to describe , i use some command to export the function,
Eg:
I define a cpp file named shell.cpp , and I define a function named freertosTaskList, then I use

const char shellCmdtl[] = "tl";
const char shellDesctl[] = "List freeRTOS task status";
const ShellCommand shellCommandtl SECTION("shellCommand") = 
{
    .attr.value = 1,
    .data.cmd.name = shellCmdtl,
    .data.cmd.function = (int (*)())freertosTaskList,
    .data.cmd.desc = shellDesctl,
};

it reports error, but if i change the shell.cpp to shell.c it works fine?

This kind initialization (starting on line 3 in your code snippet) is supported by C but not by C++. In C++, it would look like:

ShellCommand shellCommandtl SECTION("shellCommand");
memset(&shellCommandtl, 0, sizeof(shellCommandtl);
shellCommandtl .attr.value = 1;
shellCommandtl .data.cmd.name = shellCmdtl;
shellCommandtl .data.cmd.function = (int (*)())freertosTaskList;
shellCommandtl .data.cmd.desc = shellDesctl;

Note that the constness is lost.

I’m unsure what SECTION("shellCommand") is. If it assigns the struct to a unmodifiable section, then it won’t work.

Thank you for your reply, but init the struct out of the function is legal, like the picture .
image

Struct A is build without no warnings! You put the init into the function can work, but I think it is a solution but not the reason,
OK, do not consider about the “SECTION” ,let’s see this, in fact, it’s a feature here .
image
We can use the macro to export command directly, and the macro expand like this
image

As I said, if I change the file type to C, it will be OK, but CPP it’s error, but as my first picture struct can init when it created.

Thank you very much, and I am not good at English, Sorry!

I haven’t been very precise about what’s not supported in C++.

Struct initialization is supported in C++ but very restricted compared to C. If I’m not mistaken, it’s restricted to:

  • one level only
  • all properties must appear in order
  • certain restrictions on the type of properties
  • all properties should be listed

Since your struct has three levels (like data / cmd / name), it’s not supported by C++.

I’ve checked it in the standard. You should be able to make it work with braces:

const char shellCmdtl[] = "tl";
const char shellDesctl[] = "List freeRTOS task status";
const ShellCommand shellCommandtl SECTION("shellCommand") = 
{
    .attr = { .value = 1},
    .data = {
        .cmd = {
            .name = shellCmdtl,
            .function = (int (*)())freertosTaskList,
            .desc = shellDesctl
        }
    }
};

thank you very much, you really help me slove my problem, I am very happy!!!