Sunday, 11 November 2018

static use on other file compile together throws error

Static file:


  • Even if we give 2 .c file into the gcc, it will compile upto Assembler as a single .c file.
  • The below output shows that Preprocessor gave .i for 2 files.
    • Compiler gave the .s for 2 files.
    • Assembler gave the .o for 2 fileHence linker only combine these 2 .o files into a single executable file.
    • Hence we could not use the static variable in other .c file because each .c file compile separately upto .o
    • NO_STAT flag is used to remove the extern on 2nd file and make an global variable,
      • so that 2 variable are different variable even with the same name.
  • ------------ snip ---------------
  •  -r-- 1 manirathinam manirathinam   197 Nov 11 03:28 static_1.c
    -rw-rw-r-- 1 manirathinam manirathinam   171 Nov 11 03:30 static_2.c
    -rw-rw-r-- 1 manirathinam manirathinam 18382 Nov 11 03:30 static_1.i
    -rw-rw-r-- 1 manirathinam manirathinam  1174 Nov 11 03:30 static_1.s
    -rw-rw-r-- 1 manirathinam manirathinam  1392 Nov 11 03:30 static_1.o
    -rw-rw-r-- 1 manirathinam manirathinam 18398 Nov 11 03:30 static_2.i
    -rw-rw-r-- 1 manirathinam manirathinam   853 Nov 11 03:30 static_2.s
    -rw-rw-r-- 1 manirathinam manirathinam  1244 Nov 11 03:30 static_2.o
    -rwxrwxr-x 1 manirathinam manirathinam  7312 Nov 11 03:30 a.out
    manirathinam@manirathinam-Aspire-E1-431:~/velraj/clan$
  •  
  • ---------------- snip --------------


Program:

File 1: static_1.c


/* static on 2 file compile together by Velraj.K
   Check : http://velrajcoding.blogspot.in
 */

#include <stdio.h>

static int stat_value = 0;
// we can inclide the .c file it can able to compile successfuly.
//#include "static_2.c"



int main()
{
        printf("Inside main \n");

        return 0;
}


/* Compile :
 * Gcc working for 2 .c file:
 *     Even if we give 2 .c file into the gcc, it will compile upto Assembler as a single .c file.
 *     The below output shows that Preprocessor gave .i for 2 files.
 *                                 Compiler gave the .s for 2 files.
 *                                 Assembler gave the .o for 2 files
 *                                 Hence linker only combine these 2 .o files into a single executable file.
 *                   Hence we could not use the static variable in other .c file because each .c file compile separately upto .o
 *                   NO_STAT flag is used to remove the extern on 2nd file and make an global variable,
 *                       so that 2 variable are different variable even with the same name.
 *
 manirathinam@manirathinam-Aspire-E1-431:~/velraj/clan$ ls -ltr
total 16
-rw-rw-r-- 1 manirathinam manirathinam  197 Nov 11 03:28 static_1.c
-rw-rw-r-- 1 manirathinam manirathinam  171 Nov 11 03:30 static_2.c
-rwxrwxr-x 1 manirathinam manirathinam 7312 Nov 11 03:30 a.out
manirathinam@manirathinam-Aspire-E1-431:~/velraj/clan$ gcc static_1.c static_2.c --save-temp
manirathinam@manirathinam-Aspire-E1-431:~/velraj/clan$  ls -ltr
total 72
-rw-rw-r-- 1 manirathinam manirathinam   197 Nov 11 03:28 static_1.c
-rw-rw-r-- 1 manirathinam manirathinam   171 Nov 11 03:30 static_2.c
-rw-rw-r-- 1 manirathinam manirathinam 18382 Nov 11 03:30 static_1.i
-rw-rw-r-- 1 manirathinam manirathinam  1174 Nov 11 03:30 static_1.s
-rw-rw-r-- 1 manirathinam manirathinam  1392 Nov 11 03:30 static_1.o
-rw-rw-r-- 1 manirathinam manirathinam 18398 Nov 11 03:30 static_2.i
-rw-rw-r-- 1 manirathinam manirathinam   853 Nov 11 03:30 static_2.s
-rw-rw-r-- 1 manirathinam manirathinam  1244 Nov 11 03:30 static_2.o
-rwxrwxr-x 1 manirathinam manirathinam  7312 Nov 11 03:30 a.out
manirathinam@manirathinam-Aspire-E1-431:~/velraj/clan$
manirathinam@manirathinam-Aspire-E1-431:~/velraj/clan$ gcc static_1.c static_2.c --save-temp -DNO_STAT
static_2.o: In function `add':
static_2.c:(.text+0x19): undefined reference to `stat_value'
collect2: error: ld returned 1 exit status
manirathinam@manirathinam-Aspire-E1-431:~/velraj/clan$
*/


File 2: static_2.c


/* static on 2 file compile together by Velraj.K
   Check : http://velrajcoding.blogspot.in
 */

#include <stdio.h>


#ifdef NO_STAT
extern int stat_value;
#else
int stat_value;
#endif


int add()
{
        int value = 100;
        value = stat_value + value;

        return value;
}

No comments:

Post a Comment