下列錯誤,可能是因爲在64位上跑32位程序:
Value too large for defined data type |
此錯誤對應的出錯代碼爲EOVERFLOW,原因可能是目標文件超過2GB大小。
下列代碼可能會導致這個錯誤出錯(爲何說是可能,本節最後部分解釋):
// g++ -g -o x x.cpp -m32 #include <errno.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <string>
int main(int argc, char* argv[]) { struct stat st;
if (stat(argv[1], &st) != 0) { printf("stat failed: %s.\n", strerror(errno)); return 1; } else { printf("%zd\n", st.st_size); return 0; } } |
改成下列後,運行正常:
// g++ -g -o x x.cpp -m32 #include <errno.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <string>
int main(int argc, char* argv[]) { struct stat64 st;
if (stat64(argv[1], &st) != 0) { printf("stat failed: %s.\n", strerror(errno)); return 1; } else { printf("%zd\n", st.st_size); return 0; } } |
前面說的“可能”,是因爲不同機器的編譯環境(可理解爲默認編譯參數)可能並不相同,因此導致結果是可能,原因是宏“-D_FILE_OFFSET_BITS=64”會影響結果,如果定義了,則效果如同最後一段代碼,否則報錯“Value too large for defined data type”。相關宏:_LARGEFILE64_SOURCE和__USE_FILE_OFFSET64,相關LIBC頭文件:features.h。
一些引用到的第三方庫,可能定義了FILE_OFFSET_BITS,使用時需注意,比如:
# grep "FILE_OFFSET_BITS" /usr/include/*/*.h /usr/include/bits/environments.h:#define __ILP32_OFFBIG_CFLAGS "-m32 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64" /usr/include/mysql/my_config_x86_64.h:#define _FILE_OFFSET_BITS 64 /usr/include/python2.7/pyconfig-64.h:#define _FILE_OFFSET_BITS 64 /usr/include/python3.4m/pyconfig-64.h:#define _FILE_OFFSET_BITS 64 |
附1:查看GCC默認機器相關編譯參數
gcc -march=native -c -Q --help=target |
附2:查看GCC默認定義的宏
gcc -posix -E -dM - </dev/null |
或:
cpp -dM /dev/null |