Have searched enough answers but none of the solutions work for me.
scenario : I am trying to include a .h file that has some functions declared (not defined) and some variables declared.
If I include this header file in the source files (2 to be precise) that actually use the functions and variables, then the last one that compiles has a linker error that states
undefined reference to `abc::myfun(char const*, char const*, char*)'
All the functions and variables in the header files have been declared as extern and include guards are present.
I wish to have one cpp file put a value in the variable defined in the .h file and another cpp file to be able to read it.
Also if it helps, every piece of my code is in a namespace that I have defined ( namespace abc{ //all my code, in all the files }
)
Your error seems to be with
abc::myfun(char const*, char const*, char*)
. I was able to reproduce and confirm this. Without the second part you'll get the error.I'm guessing you have a header file something like
and a .cpp source file needs
Declarations in your
.h
file are doing exactly that - letting compiler know of the objects you are planning on declaring "somewhere". That "somewhere" would be a compilation unit (a.c
or.cpp
file) where that variable would be defined.Here's an example (skipping the guards for simplicity):
foo.h
:foo.c
:main.c
:As paddy mentioned above - make sure you are not accidentally nesting namespaces, since
abc::something
is not the same asabc::abc::something