77

Two patches queued into the Linux kernel's build system development tree, kbuild-next, would enable the -fms-extensions compiler argument everywhere for allowing GCC and LLVM/Clang to use the Microsoft C Extensions when compiling the Linux kernel. Being in kbuild-next these patches will likely be submitted for the Linux 6.19 kernel merge window next month but remains to be seen if there will be any last minute objections to this change.

The -fms-extensions compiler option honored by the GNU Compiler Collection and LLVM/Clang allow enabling some non-standard C/C++ constructs used within Microsoft header files and honored by the the Microsoft Visual C/C++ compiler. For Linux kernel development purposes, enabling the Microsoft C Extensions would allow including a tagged struct or union anonymously in another struct/union.

you are viewing a single comment's thread
view the rest of the comments
[-] entwine@programming.dev 72 points 1 month ago

I'm sitting around doing IT shit waiting things to download/backup/install/etc and have nothing better to do, so here's an AI-free explanation with code samples:

It's basically just a code style thing. Standard C allows you to declare unnamed structs/unions within other structs/unions. They must be unnamed, so it'd look like this:

struct test {
        int a;
        struct {
                char b;
                float c;
        };
        double d;
};

Which is fine, but the -fms-extensions flag enables you to do the same thing with named structs. For example:

struct test {
        int a;
        struct test2 {
                char b;
                float c;
        };
        double d;
};

without -fms-extensions, the above will compile, but won't do what you might assume. b and c will be members of struct test2, not test. So something like this won't compile:

struct test my_test;
my_test.b = 1; // error: ‘struct test’ has no member named ‘b’

But with the flag, not only does it work, it also lets you do some convenient things like this:

struct test2 {
        char b;
        float c;
};
struct test {
        int a;
        struct test2;
        double d;
};
//...
struct test my_test;
my_test.b = 1; //OK

That is, you can reuse an existing struct definition, which gives you a nice little tool to organize your code.

Source: https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html

[-] iloveDigit@piefed.social 8 points 1 month ago

Nice, thank you

[-] ijhoo@lemmy.ml 8 points 4 weeks ago

If this is so convenient, why wasn't it made a part of a newer C standard?

[-] entwine@programming.dev 3 points 4 weeks ago

It's not that convenient. I can't even think of a situation where this would be useful for structs, only unions. And in the case of unions, you usually want to keep them as small as possible (or better yet, avoid them altogether).

But besides that, C is a language that tends to prefer minimalism. Using macros, you can accomplish a similar thing already, even if it's not as nice.

[-] DeltaWingDragon@sh.itjust.works 1 points 4 weeks ago

What's the point of this? If you have a struct within a struct, you probably want them to nest. The -fms-extensions will un-nest them, which is not what you mean.

// If I type this:
struct test {
        int a;
        struct test2 {
                char b;
                float c;
        };
        double d;
};

struct test my_test;

// I want this to happen:
my_test.test2.b = 'x'; //assigning the members of the nested struct
my_test.test2.c = 3.141; //this will work
printf("%f", my_test.c); //this will NOT work since c is a property of test2
[-] entwine@programming.dev 1 points 4 weeks ago

You can already do that in standard C like this:

struct test {
        int a;
        struct {
                char b;
                float c;
        } test2;
        double d;
};

I can't think of any particular reason why you'd want an unnamed struct inside a struct, but you definitely would want to be able to have an unnamed struct inside a union. I suspect the struct-inside-struct thing can become useful in some scenarios involving unions.

[-] DeltaWingDragon@sh.itjust.works 1 points 3 weeks ago

Does that really have the same results as the example scenario I described? How would you even access the unnamed struct, since it is unnamed?

[-] entwine@programming.dev 1 points 3 weeks ago* (last edited 3 weeks ago)

The same way you did, via the name of the member: my_test.test2.b = 'x';

The unnamed struct provides the type for a member named test2. Doing it this way saves you the trouble of defining the struct externally and giving it a name. It's identical to this, except in this example you can reuse the struct definition:

struct crappyname {
    char b;
    float c;
};
struct test {
    int a;
    struct crappyname test2;
    double d;
};
this post was submitted on 09 Nov 2025
77 points (96.4% liked)

Linux

10505 readers
621 users here now

A community for everything relating to the GNU/Linux operating system (except the memes!)

Also, check out:

Original icon base courtesy of lewing@isc.tamu.edu and The GIMP

founded 2 years ago
MODERATORS