E35 Answer


No, this still does not work.

The compiler mangages to compile both source files, but the variables height and width in mainRect.c. are described as being externally defined. The linker, however, cannot find them (because the variables in rect.c have internal linkage.)


/* --- rect.h --- */ void setWidth( int w ); void setHeight( int h ); int getArea();
/* --- rect.c --- */ static int width = 0; static int height = 0; void setWidth( int w ) { width = w; } void setHeight( int h ) { height = h; } int getArea() { return height*width; }
/* --- mainRect.c --- */ #include "rect.h" extern int height; extern int width; int main() { height = 4; width = 3; printf("Area: %d\n", getArea() ); system("pause"); }

This is like trying to access the private instance variables of an object.