Extern const char * behavior in C ++

In the following example:

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";

      

When compiling sources, I got the following errors:

$ 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

      

I know that in C ++ a constant implies internal linkage, but why is there no original undefined error for szVers?

+3


source to share


1 answer


Update your compiler. With GCC 7.1.0 I can reproduce what you are getting, but with a newer version I get an error for all three variables:



enter image description here

0


source







All Articles