c++ - calling my namespace with std together from another cpp file -
c++ - calling my namespace with std together from another cpp file -
i have main.cpp , example.cpp files.
this might bad did, since i'm new c++, here code wrote within example.cpp:
#include <iostream> namespace illustration { using namespace std; void myfunc1() { cout << "func1" << endl; } void myfunc2() { cout << "func2" << endl; } } so, came thought phone call namespace within main.cpp:
#include <iostream> using namespace example; int main() { homecoming 0; } now have problem phone call "example" namespace file. need create header cpp file ?
here's how should do. should declare functions intended included in namespace in single header file, , include in every cpp file needs it. implementations of "namespaced" functions should in single cpp file too. example, should followed.
example.hh :
#ifndef __example_hh__ # define __example_hh__ namespace illustration { void myfunc1(); void myfunc2(); } #endif example.cpp :
#include <iostream> #include "example.hh" void example::myfunc1() { std::cout << "func1" << std::endl; } void example::myfunc2() { std::cout << "func2" << std::endl; } once illustration namespace declared , implemented, can utilize followed :
main.cpp :
#include "example.hh" int main() { example::myfunc1(); example::myfunc2(); homecoming (0); } note don't utilize using namespace statement , that's why need write example:: before name of functions. if want not have write example:: each time, can write using namespace example careful : doesn't not allow ommit #include "example.hh" preprocessor directive otherwise compiler not know example namespace exists.
so remember : using namespace doesn't stand #include directive.
an explanation source files , header files.
in example, have 3 files : 1 header file (example.hh), , 2 source files (example.cpp, main.cpp). it's mutual (because useful) declare namespaces, classes, structures, functions ... in header file, , include header file in every source file needs utilize 1 of thoses defined names.
why ? allow compiler know names. here, create executablefile , there 2 steps :
step 1 : compile every single source file corresponding object file, i.e create example.o file example.cpp file. example.o file contain byte code (here, code of example::myfunc1 , example::myfunc2 functions) , main.o code main function.
these object files made compiler. example, create object file gcc (g++ c++) followed : g++ -c -o example.o example.cpp.
it's here header files useful. when example.cpp file read gcc, gcc reads #include "example.hh" statement tells him read example.hh too, , that's how gcc can know namespace , functions.
example.cpp, if had not included header file, gcc have read void example::myfunc1() { ... } , have thrown error telling unknown namespace depending on compiler use. for main.cpp, have got error telling namespace not found. what interesting in way of doing in compile main.o, byte code says call myfunc1 illustration namespace, phone call myfunc2 illustration namespace does not include code these 2 functions in main.o file.
so when gcc reads function calls, not know implementation knows them header file. roughly, declaring them in header file , include in source files tells gcc this namespace , functions exist, implemented somewhere else.
so here, have 2 corresponding object files : main.o , example.o.
step 2 : take object files you've made link them , create executable. again, gcc : gcc -o exampleexcutable main.o example.o. that's magic happens : linker see there's phone call example::myfunc1 , example::myfunc2, finds implementation in example.o, , able create executable file.
here executable file exampleexecutable, run , expected output :
$ ./exampleexecutable func1 func2 to concude, header files used declare symbols , included in source files. when sources files compiled, corresponding object files don't contain definition of these declared symbols, they're resolved during link time.
c++ namespaces
Comments
Post a Comment