3
在下面的例子:行爲
file1.c中:
#include <stdlib.h>
#include <stdio.h>
extern const char* szVers;
extern const int iTest;
extern const char cText[];
int main(int argc, char** argv)
{
printf("Result = %s - %d - %c\n", szVers, iTest, cText[0]);
return (0);
}
file2.c中
const char* szVers = "Version 1.0";
const int iTest = 6;
const char cText[] = "ABCD";
當我編譯源代碼,我得到了以下錯誤:
$ g++ -o test file1.c file2.c
/tmp/cctVd57Y.o: In function `main':
file1.c:(.text+0x12): undefined reference to `cText'
file1.c:(.text+0x1b): undefined reference to `iTest'
collect2: ld returned 1 exit status
我知道在C + + 常量意味着內部鏈接,但爲什麼沒有一個未定義的引用錯誤szVers?
它可以幫助https://stackoverflow.com/questions/14977058/extern -const-char-const-some-constant-giving-me-linker-errors –
@HoàngĐăng:字面意思相反 –