c++ - 错误 'extern' 及更高版本 'static'

标签 c++ static extern

如果我在名为 ppmformat.h 的文件中声明一个函数

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

...并在 ppmformat.cpp 中定义

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

我收到以下错误:

'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
    {
    ....
    }
  }

最佳答案

声明具有不同链接的同一实体的代码无效。

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.

在实践中,g++ 5.1.4 拒绝编译它,而不幸的是 Visual C++ 2015 接受它作为语言扩展,并带有警告:

[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]
> _

对于接受此警告的编译器,最好将该警告转换为硬错误。对于 Visual C++,该选项为 /we4211

关于c++ - 错误 'extern' 及更高版本 'static',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40419244/

相关文章:

c++ - static constexpr 函数与全局函数不同?

c++ - 与第三方公共(public)图书馆/代码接口(interface)

c++ - 在 CMake 项目中从 C++ 调用 C 代码。 undefined symbol 。有外部C

c++ - "The Rule of Zero"是否也适用于具有虚方法的类?

js-test-driver 不提供静态文件

C++11 lambdas 到函数指针

python - 用闭包模拟python中的静态变量

objective-c - Objective-C 中的静态、外部和内联

c++ - 将 string::iterator 转换为 std::string

c++ - 讨厌的链接错误