https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104397

            Bug ID: 104397
           Summary: Wrong warning message about global static variable in
                    module(.hpp, .cpp)
           Product: gcc
           Version: 9.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bogdasar1985 at gmail dot com
  Target Milestone: ---

Created attachment 52355
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52355&action=edit
Output of "gcc -v -save-temps all-your-options source-file && neofetch
--stdout"

class.hpp
-------------
#ifndef CLASS
#define CLASS
static int a;
class Class
{
public:
    Class();
    ~Class();
private:
    int b;
    int c;
};
#endif

class.cpp
---------------
#include "class.hpp"
#include <stdio.h>
Class::Class() : b(0), c(0)
{
    a++;
    printf("%d\n", a);
};

Class::~Class()
{
    printf("%d\n", a);
    a--;
}

main.cpp
-------------
#include "class.hpp"
int main()
{
    Class a, b, c, d;
    return 0;
}


Compiling that by command:
g++ -Wall class.hpp class.cpp main.cpp -o main

generate next warning:

class.hpp:3:12: warning: ‘a’ defined but not used [-Wunused-variable]
    3 | static int a;
      |            ^

which is wrong, because variable a used for output.

Output of
gcc -v -save-temps all-your-options source-file && neofetch --stdout
in attachment file.

class.ii
------------
# 1 "class.hpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "class.hpp"


static int a;
class Class
{
public:
    Class();
    ~Class();
private:
    int b;
    int c;
};

main.ii
-------------
# 1 "main.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "main.cpp"
#pragma GCC pch_preprocess "class.hpp.gch"
int main()
{
    Class a, b, c, d;
    return 0;
}

Reply via email to