C++ 中 extern const char* 的行为

Behaviour of extern const char* in C++

在下面的例子中:

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++ 中 const 意味着内部链接,但为什么没有对 szVers 的未定义引用错误?

更新你的编译器。使用 GCC 7.1.0 我可以重现你得到的东西,但是使用较新的版本我得到所有三个变量的错误: