madrugadas25845
madrugadas25845

Reputation: 25

error 'extern' and later 'static'

If i declare a function in a file named ppmformat.h

//file ppmformat.h
namespace imaging
{
Image * ReadPPM(const char * filename);
} //namespace imaging

... and define it in ppmformat.cpp

static imaging::Image * imaging::ReadPPM(const char *filename)
{
 ....
}

I get the following error:

'imaging::Image* imaging::ReadPPM(const char*)' was declared 'extern' and later 'static' [-fpermissive]

    //ppmformat.h
    #ifndef _PPM
    #define _PPM

    #include "Image.h"
    namespace imaging
    {
    /*! Reads a PPM image file and returns a pointer to a newly allocated                  Image object containing the image.
    *
    *   \param filename is the null-terminated string of the name of the  file to open.
    *
    *  \return a pointer to a new Image object, if the read operation was successful, nullptr otherwise.
    */
    Image * ReadPPM(const char * filename);
    } //namespace imaging

    #endif

   //ppmformat.cpp
   #include <iostream>
   #include<string.h>
   #include<stdio.h>
   #include<stdlib.h>
   #include <string>
   #include <fstream>
   #include "ppmformat.h"

   using namespace std;

   imaging::Image * imaging::ReadPPM(const char *filename)
   {
   ......
   }

   //Image.h

   #ifndef _IMAGE
   #define _IMAGE

   #include "Color.h"
   #include <iostream>

   namespace imaging
   {
    class Image
    {
    ....
    }
   }

  //Image.cpp
  #include <iostream>
  #include "Color.h"
  #include "Image.h"
  ....
  //end of Image.cpp

  //Color.h
  #ifndef _COLOR
  #define _COLOR
  namespace imaging
  { 
    Class Color
    {
    ....
    }
  }

Upvotes: 2

Views: 5391

Answers (1)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145389

Code that declares the same entity with different linkages is invalid.

C++11 §7.11/8 (dcl.std/8):

The linkages implied by successive declarations for a given entity shall agree. That is, within a given scope, each declaration declaring the same variable name or the same overloading of a function name shall imply the same linkage. Each function in a given set of overloaded functions can have a different linkage, however.

For the in-practice, g++ 5.1.4 refuses to compile it, while unfortunately Visual C++ 2015 accepts it as a language extension, with a warning:

[C:\my\forums\so\257]
> g++ foo.cpp
foo.cpp: In function 'imaging::Image* imaging::ReadPPM(const char*)':
foo.cpp:9:62: error: 'imaging::Image* imaging::ReadPPM(const char*)' was declared 'extern' and later 'static' [-fpermissive]
 static imaging::Image * imaging::ReadPPM(const char *filename)
                                                              ^
foo.cpp:5:13: note: previous declaration of 'imaging::Image* imaging::ReadPPM(const char*)'
     Image * ReadPPM(const char * filename);
             ^
foo.cpp: At global scope:
foo.cpp:9:54: warning: unused parameter 'filename' [-Wunused-parameter]
 static imaging::Image * imaging::ReadPPM(const char *filename)
                                                      ^

[C:\my\forums\so\257]
> cl foo.cpp
foo.cpp
foo.cpp(10): warning C4211: nonstandard extension used: redefined extern to static
foo.cpp(9): warning C4100: 'filename': unreferenced formal parameter
foo.cpp(9): warning C4505: 'imaging::ReadPPM': unreferenced local function has been removed

[C:\my\forums\so\257]
> _

With a compiler that accepts this with a warning, it can be a good idea to turn that warning into a hard error. With Visual C++ that's option /we4211.

Upvotes: 2

Related Questions