Reputation: 53
I would like to know how to fix this error:
error: aggregate 'Padron_Electoral::getFileSize(const char*)::stat stat_buf' has incomplete type and cannot be defined
The method in question is:
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;
}
If I comment this method the rest of the code compiles, Im using GNU GCC compiler. Both computers have the same compiler and Im working in Codeblocks. If I try to compile it on console it throws the same error. Has anyone got this same mistake? What could be the reasons and how to fix it?
Code is identical in both computers, #include #include and the include of the .h file
Other errors it throws:
error: invalid use of incomplete type 'struct Padron_Electoral::getFileSize(const char*)::stat'
error: forward declaration of 'struct Padron_Electoral::getFileSize(const char*)::stat'
The #include could be included on the computer where the code compiled but on the other computer the include throws errors:
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))
Operating system of the computer where the code fails to compile is: Windows 7 Ultimate.
Processor of the computer where the code fails to compile: Intel Core i5 4440 CPU 3.10GHz
MinGW. GCC 4.8.1 (both computers)
IDE: Codeblocks 13.12 (both machines use this IDE)
Both are 64 bit Operating Systems
Operating system of the computer where it compiles and works: Windows 7 Home Premium.
Processor of the computer where it compiles and works: Intel Pentium CPU P6100 2.0GHz
Thank you
Edit: Code compiles now, MinGW32 issue
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;
}
Question remains on why does MinGW throws an error on this method on one computer and not in the other.
Upvotes: 3
Views: 5516
Reputation: 53
Code compiles now, MinGW 32 bits on a 64 bits issue. Hope this helps someone else
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;
}
Thank you all for the help and specially to @kriss , answer based on Determine 64-bit file size in C on MinGW 32-bit
Upvotes: 2
Reputation: 24207
Where is the struct stat defined?
The compiler is telling you he couldn't find the definition of stat, and therefore stat_buf has an incomplete type (wich means it can only be defined as a pointer).
What it implies is that there is some difference at compile time between the two computers where you are compiling.
To pinpoint the problem, you should try to isolate the problem in a simpler program and try to compile it and run it on both systems. Something like below could be enough:
#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;
}
It is a complete compiling program, and you should be able to post in the question the compiling errors you get on both target systems.
Explicitely stating both target systems (which obviously are not identical) could also help: - compiler version - operating system - processor - actual compilation line and/or makefile if possible.
For instance such problems could arise because of some missing define like _POSIX_C_SOURCE
Spoting such problems is usually easy enough if you give enough informations.
EDIT: After checking it appears that the problem was related to mingw. Specifically using MingW32 on a 64 bits system. A possible solution was available here: 64 bits file size on Mingw32
Upvotes: 2