What is the difference between
andCode:void func();
I have a Codeblocks project with two files, main.cpp and main2.cpp.Code:static void func();
main.cpp
main2.cppCode:#include <iostream> #include"main2.cpp" using namespace std; void fnc_in_main(){ cout<<"This is fnc_in_main() in main.cpp"<<endl; } int main() { cout << "This is main() in main.cpp" << endl; fnc_in_main(); fnc_in_main2(); return 0; }
in main.cpp, if I don't writeCode:#include <iostream> using namespace std; static void fnc_in_main2(){ cout<<"This is fnc_in_main2() in main2.cpp"<<endl; }
the compiler gives an error saying fnc_in_main2() is not definedCode:#include"main2.cpp"
in main2.cpp, if I do not write
the compiler says fnc_in_main2() has been declared multiple times and says the multiple declaration occurred in main2.cpp.Code:static void fnc_in_main2(){
However, it says here that declaring a function static makes it visible only in the file in which the function was declared, but in my program it looks like making it static actually caused it to be visible in the other file main.cpp.
Could someone please give me any idea about static functions and working with multiple files? Also, I think static functions inside a class works differently right?
Again, I did the compiling from codeblocks in this case. If I have to compile multiple files in gcc from the command line, how would I give the command?
Thanks in advance



1Likes
LinkBack URL
About LinkBacks


