Sheer
2012-07-27 10:52:07 UTC
//major.c
#include
#include "minor.c"
extern void display();
int main()
{
display();
return 0;
}
--------------------
//minor.c
void display()
{
printf("Hello Yahoo");
}
If I compile it from the command prompt using "gcc -o major.exe major.c minor.c" then it kinda appends minor.c to major.c(just as we want) and compiles it as one file into major.exe.Also,if I compile "major.c" from Codeblocks GUI, then again it compiles fine and generates major.exe.Now PLEASE see if you can clarify the following for me:
1)Do we need any function, whose definition is in another file (but part of same program) to be tagged "extern"? The above program works fine even if we don't use "extern" while declaring display() in "major.c".One contributor had told me that "extern" tells the compiler that the definition of the function is in another file.But why then it works without extern?And if commands like "gcc -o major.exe major.c minor.c" and "#include "minor.h" essentially joins together files that constitute the program, then why we need extern AT ALL(for functions as well as variables) as after all, we are essentially dealing with one large file and the "definition in another file" thing means little?
2)In the above code, it works fine if I compile "major.c".But when I compile minor.c, it throws some errors quite expectedly as the prinf() there has no prototype (as there is no stdio.h there).Had minor.c been a large file, there might be many more errors.Can you tell me how we deal with this scenario? I mean,USING THE GUI how do we compile the files of the program which don't have the main() function to check if they are correct and have no errors?Say my program had 5 files, how do I compile those which don't contain the main() function? In this case,will it be wrong for me to use "#include
3)Do we need to use "extern" (if at all it's necessary) and "#include "minor.c" in "major.c" even if we are using the "Project mode" option in Codeblocks Gcc GUI?Or in Project mode we can use multiple files for the program as if they are part of one large file without worrying about hash-including or using "extern", and it's the IDE's job to join them together?