This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Making global variables static (-h option)



Hi,


I'm having some trouble understanding the behavior of the -h option of the cl6x compiler:

--make_static,-h             Make all global symbols static


I'm trying to compile and link the following code:


one.c

====

struct {
    char infot, iounit;
    char ok, lerr;
    char po;
} infoc_;

int a;

void initvalue1(void){
  a = 1;
}

void printvalue1(void){

  printf( "%d", a );

}

two.c

====

struct {
    int infot, unit;
    int ok, lerr;
} infoc_;

int a;

void initvalue2(void){
  a = 2;
}

void printvalue2(void){

  printf( "%d", a );

}

three.c

====

int main(void){

  initvalue1();
  initvalue2();

  printvalue1();
  printvalue2();

  return 0;

}

My understanding is that, using the following lines for compiling/linking, variables infoc and a should be made static, and thus this should work with the following lines:

cl6x -c one.c
cl6x -c two.c
cl6x -c three.c
cl6x -z -h one.obj two.obj three.obj -o allstatic.out


However, I am still getting linking errors:

<Linking>
error: symbol "_infoc_" redefined: first defined in "one.obj"; redefined in
   "two.obj"
error: symbol "_a" redefined: first defined in "one.obj"; redefined in
   "two.obj"
error: errors encountered during linking; "allstatic.out" not built


Am I misunderstaiding the behaviour of the -h option for the linker? Variables a and infoc sholud be considered static when linking, is that correct?

Thanks in advance.

  • Francisco Igual said:
    Am I misunderstaiding the behaviour of the -h option for the linker?

    Yes.

    Francisco Igual said:
    Variables a and infoc sholud be considered static when linking, is that correct?

    No.

    Your central misunderstanding is on when the change from global symbol to static symbol occurs.  Using -h has no effect on the scope of the symbols in the input files.  When the link starts, the symbols _infoc_ and _a (these same symbols are infoc_ and a when written in C) are global.  That's why you see the redefined error messages.  Supposing the link succeeds, -h causes all the global symbols in the output file to change from global to static.  

    Thanks and regards,

    -George