Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual C++ lambdas always output debug information

Tags:

visual-c++

If I instantiate a lambda somewhere (and the compiler doesn't inline it), I can find a string showing where the lambda is located in my c++ code like this:

... ?AV<lambda_1>@?0??MyFunction@MyScopedClass@MyNamespace@@SAXXZ@ ...

I don't want this information in the executable, as it could give away important names of classes and functions.

All kinds of output debug information are turned off. If I use a normal function instead, the final executable doesn't have this information, so manually converting all lambdas into normal functions would "fix it". But what's the best way to handle this? Can we tell the compiler to transform lambdas into normal functions?

UPDATE: I tested with other compilers: g++ and clang. They both leave the same references. I also found another unanswered question about this Gcc - why are lambdas not stripped during release build Please don't come with the "why do you care about a few symbols anyway".

Here's some code you can test with:

#include <iostream>
#include <functional>

class MyUnscopedClass
{
public:
    MyUnscopedClass(const std::function<void(int x)>& f) :
        f(f)
    {

    }
    std::function<void(int x)> f;
};

namespace MyNamespace
{
    class MyScopedClass
    {
    public:
        static void temp1(int x)
        {
            std::cout << x * (x + 1);
        }

        static void MyFunction()
        {
            //MyUnscopedClass obj(temp1); // no symbols
            MyUnscopedClass obj([](int x) // ?AV<lambda_1>@?0??MyFunction@MyScopedClass@MyNamespace@@SAXXZ@
                {
                    std::cout << x;
                });

            obj.f(23);
        }
    };
}

int main()
{
    MyNamespace::MyScopedClass::MyFunction();
}
like image 769
user1000 Avatar asked Dec 10 '25 02:12

user1000


1 Answers

With the help of @dxiv in the comments, I found the problematic setting.

Configuration Properties > General > C++ Language Standard

can't be, for some reason,

Preview - Features from the Latest C++ Working Draft (std:c++latest)

So I set it to the second most recent one

ISO C++17 Standard (std:c++17)

and I get a random identifier instead.

AV<lambda_f65614ace4683bbc78b79ad57f781b7f>@@

I'm still curious how this identifier is chosen though.

like image 53
user1000 Avatar answered Dec 13 '25 19:12

user1000



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!