c++ - Trying to understand the extern keyword -
i trying learn extern keyword.
i created file try2.cpp
#include <stdio.h> #include <conio.h> extern int a; int main() { = 5; printf("%d", a); getch(); return 0; } and 1 try1.cpp
int a; int main() { = 10; return 0; } but getting error in try2.c undefined. both files in bin folder of turboc.
what problem?
in try2.cpp, have:
extern int a; this tells compiler a defined externally - i.e. not within try2.cpp. so, must define externally, , need second .cpp file - lets make a.cpp file contains:
int a; you can compile each of try2.cpp , a.cpp:
tc -c try2.cpp tc -c a.cpp then need link them, specifying path cs.lib standard library file (search windows explorer if below doesn't work - updating \tc\lib\ path below):
tlink try2.obj + a.obj, program.exe, , \tc\lib\cs.lib that should create new program.exe run....
you should better compiler though....
Comments
Post a Comment