Bạn có chắc chắn muốn xóa bài viết này không ?
Bạn có chắc chắn muốn xóa bình luận này không ?
Lỗi liên quan đến case-sensitive của tên file trong C
Case-sensive khi biên dịch
Cấu trúc source ví dụ
Giả sử có source như này:
.
├── hello
├── hello.c
├── inc_lowercase
│ └── abc.h
└── inc_uppercase
└── Abc.h
hello.c:
$ cat hello.c
#include<stdio.h>
#include"Abc.h"
#include"abc.h"
int main(int argc, char* argv[])
{
abc_t var1;
Abc_t var2;
var1.x = 0;
var2 = 3;
printf("OMG Hello world %d %ld\n",var1.x, var2);
return 0;
}
inc_lowercase/abc.h:
typedef struct _abc_
{
int x;
}abc_t;
*inc_uppercase/Abc.h:
typedef long int Abc_t;
2. So sánh thử
2.1 Thư mục source nằm trên home của VMWare
Kết quả: Build ngon, chạy tốt:
$ gcc -o hello hello.c -I./inc_uppercase -I./inc_lowercase
$ ./hello
OMG Hello world 0 3
2.1 Thư mục source nằm trên thư mục chia sẻ giữa VMWare (chạy Linux) với Host (chạy Windows) thông qua vmhgfs:
Giả sử ta để source ở /mnt/hgfs :
Kết quả: Build lỗi, đương nhiên là không chạy được.
duser@192.168.70.225:/mnt/hgfs$ gcc -o hello hello.c -I./inc_uppercase -I./inc_lowercase
In file included from hello.c:3:0:
abc.h:1:16: error: redefinition of ‘struct _abc_’
typedef struct _abc_
^
In file included from hello.c:2:0:
Abc.h:1:16: note: originally defined here
typedef struct _abc_
^
In file included from hello.c:3:0:
abc.h:4:2: error: conflicting types for ‘abc_t’
}abc_t;
^
In file included from hello.c:2:0:
Abc.h:4:2: note: previous declaration of ‘abc_t’ was here
}abc_t;
^
hello.c: In function ‘main’:
hello.c:8:2: error: unknown type name ‘Abc_t’
Abc_t var2;
^
hello.c:13:2: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 3 has type ‘int’ [-Wformat=]
printf("OMG Hello world %d %ld\n",var1.x, var2);
^
Ta thấy các lỗi ở đây khá là "lạ", nào là redefine, nào là unkown type name.
*Nếu gặp lỗi này lần đầu thì không phải ai cũng lý giải được. :( *
Vấn đề ở đây nằm ở /mnt/hgfs.
Có vẻ như hgfs không phân biệt Abc.h với abc.h nên khi GCC (dù chạy trên VM Ubuntu) đọc cái file này lên sẽ cùng 1 tên.
Ở đây ta thấy lỗi typedef struct _abc thì có thể đoán được rằng abc.h đã được include trước.
Có vẻ như hgfs coi 2 file header trên với cùng 1 tên abc.h.
Không chỉ có hgfs mà nhiều hệ thống file khác cũng giống như thế.
doime 30-11-2016





