在不同的计算机上编译C++中的struct stat出现问题

3
我希望你能成为一个有用的助手来翻译文本。
我想知道如何解决这个错误:
错误:聚合“Padron_Electoral :: getFileSize(const char *):: stat stat_buf”的类型不完整,无法定义
涉及的方法是:
long long const Padron_Electoral::getFileSize(const char* filename) {

     struct stat stat_buf; // This line doesnt compile en the computer Im currently working on, If tried on another computer this method compiles and work.
     int rc = stat(filename, &stat_buf);
    return rc == 0 ? stat_buf.st_size : -1;
}

如果我注释掉这个方法,其余的代码就可以编译了,我使用GNU GCC编译器。两台电脑都有相同的编译器,并且我正在使用Codeblocks进行工作。如果我尝试在控制台上编译它,它会抛出相同的错误。有人遇到过这种错误吗?可能的原因是什么,如何修复?
代码在两台计算机中完全相同,包括#include 、#include 和.h文件的包含。
它还会抛出其他错误:
error: invalid use of incomplete type 'struct Padron_Electoral::getFileSize(const char*)::stat' error: forward declaration of 'struct Padron_Electoral::getFileSize(const char*)::stat'
#include 可能已经被包含在编译代码的计算机上,但在另一台计算机上,该包含会引发错误:
Build: Debug in Prueba (compiler: GNU GCC Compiler) c:\mingw\include\io.h|301|error: 'off64_t' does not name a type c:\mingw\include\io.h|302|error: 'off64_t' does not name a type c:\mingw\include\unistd.h|65|error: 'off_t' has not been declared| C:\Users\Gabriel\Documents\Progra II\Prueba\main.cpp||In function 'int main()': C:\Users\Gabriel\Documents\Progra II\Prueba\main.cpp|7|error: aggregate 'main()::stat stat_buf' has incomplete type and cannot be defined| C:\Users\Gabriel\Documents\Progra II\Prueba\main.cpp|8|error: invalid use of incomplete type 'struct main()::stat'| C:\Users\Gabriel\Documents\Progra II\Prueba\main.cpp|7|error: forward declaration of 'struct main()::stat' Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s))
无法编译代码的计算机操作系统为:Windows 7 Ultimate。
无法编译代码的计算机处理器:Intel Core i5 4440 CPU 3.10GHz
MinGW。GCC 4.8.1(两台计算机都是如此)
IDE:Codeblocks 13.12(两台机器都使用此IDE)
两者都是64位操作系统。
可以编译并工作的计算机操作系统:Windows 7 Home Premium。
可以编译并工作的计算机处理器:Intel Pentium CPU P6100 2.0GHz
谢谢。
编辑:现在代码已经编译,MinGW32问题。
long long const Padron_Electoral::getFileSize(const char* filename) {
#if __MINGW32__
    struct __stat64 stat_buf;
    int rc = __stat64(filename, &stat_buf);
    return rc == 0 ? stat_buf.st_size : -1;

#else
struct stat stat_buf;
int rc = stat(filename, &stat_buf);
return rc == 0 ? stat_buf.st_size : -1;
#endif
return 0;
}

问题在于为什么在一个计算机上MinGW会报错,而在另一个计算机上不会报错。

5
你有 #include <sys/stat.h> 吗? - Brian Bi
如果我删除结构体,那么stat就不会在这个作用域中声明。当代码移动到另一台计算机时,该方法在同一程序中的另一台计算机上运行正常,但会抛出这些错误。 - GabrielCB
@Component10 是的,我之前在我正在工作的电脑上尝试过,现在在另一台电脑上也是如此,在这两台电脑上都会抛出错误。 - GabrielCB
在计算机上,当我删除开头的结构并保留stat stat_buf时,@Component10可以正常工作;但是如果这样做,它会抛出错误:在'stat_buf'之前应该有';'|以及错误:在此范围内未声明'stat_buf'。 - GabrielCB
让我担心的是,如果某些代码无法编译,我就不确定它是否与这里的问题相同,还是一个普通的编译器错误。 - GabrielCB
显示剩余16条评论
2个回答

2

结构体stat定义在哪里?

编译器告诉你它找不到stat的定义,因此stat_buf具有不完整的类型(这意味着它只能被定义为指针)。

这意味着,在您编译的两台计算机之间,在编译时存在某些差异

为了确定问题,您应该尝试将问题隔离在一个更简单的程序中,并尝试在两个系统上编译和运行它。像下面这样的东西可能就足够了:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main()
{
    struct stat stat_buf; // This line doesnt compile en the computer Im currently working on, If tried on another computer this method compiles and work.
    int rc = stat("testfile.txt", &stat_buf);
    return rc == 0 ? stat_buf.st_size : -1;
}

这是一个完整的编译程序,您应该能够在问题中发布在两个目标系统上收到的编译错误。 明确说明两个目标系统(这些系统显然不相同)可能也会有所帮助: - 编译器版本 - 操作系统 - 处理器 - 实际的编译命令和/或Makefile(如果可能的话)。
例如,此类问题可能会因为缺少像_POSIX_C_SOURCE这样的定义而出现。
如果提供足够的信息,通常很容易发现这些问题。
编辑后: 经过检查,问题似乎与Mingw有关。具体来说,在64位系统上使用MingW32。 可以在此处找到可能的解决方案:64 bits file size on Mingw32

代码在我的电脑上编译通过,但在其他电脑上却出现了以下错误: c:\mingw\include\io.h|301|error: 'off64_t' does not name a type| c:\mingw\include\io.h|302|error: 'off64_t' does not name a type| c:\mingw\include\unistd.h|65|error: 'off_t' has not been declared|错误:聚合体'main()::stat stat_buf'类型不完整,无法定义| |错误:无效使用不完整的类型'struct main()::stat'| 错误:'struct main()::stat'的前向声明 - GabrielCB
1
问题很可能与mingw有关。你在64位系统上使用MingW32吗?如果是,问题可能与这个有关: https://dev59.com/1GjWa4cB1Zd3GeqPtbu4 - kriss
我所包含的标头是标准的Linux标头(从man页面获取),但Mingw可能有不同的要求。 - kriss
1
另请参见此处http://sourceforge.net/p/mingw/bugs/2104/,您可以尝试# undef _____STRICT_ANSI_____,此定义可能会在编译环境中自动设置,如果您有它,则似乎一些状态结构必要的类型未定义。 - kriss
我如何识别是否拥有MinGW32?我只下载了没有分类的MinGW,但我遇到了stat和off64_t相同的问题。 - Youda008
显示剩余5条评论

2

现在代码已经编译成功,是MinGW 32位在64位系统上的问题。希望这能帮助到其他人。

long long const Padron_Electoral::getFileSize(const char* filename) {
#if __MINGW32__
struct __stat64 stat_buf;
int rc = __stat64(filename, &stat_buf);
return rc == 0 ? stat_buf.st_size : -1;

#else
 struct stat stat_buf;
 int rc = stat(filename, &stat_buf);
 return rc == 0 ? stat_buf.st_size : -1;
 #endif
 return 0;
}

感谢大家的帮助,特别是@kriss,以下是基于Determine 64-bit file size in C on MinGW 32-bit的答案。


这只解决了未声明的stat问题,但“off64_t' does not name a type”问题仍然存在。 - Youda008

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接