I am currently working on a c-framework, and I want to embed a C ++ package into it. However, there are many name conflicts. So I decided to add a namespace to the C ++ source. Now the question is, should I move #include "header.h" to the {} block namespace? I just took some time to figure out the error that was caused by the following codes.
C ++ source
In ah
#include <unistd.h> struct File { void func(int fd); };
In a.cpp
#include "ah" void File::func(int fd) { ::close( fd ); }
And I added a namespace like this
New ah
namespace MyAddedNameSpace { #include <unistd.h> struct File { void func(int fd); }; }
New a.cpp file
#include "ah" namespace MyAddedNameSpace { void File::func(int fd) { ::close( fd ); } }
And the compiler complains that :: close () was not declared.
The reason I put the #include directive inside a namespace block is because the imported C ++ package also uses the #ifndef flag to include header files as follows. Therefore, I believe that a simple way is to put all the codes in a namespace block {}
#ifndef #include <header1.h> #include <header2.h> ... #else #include <header3.h> #include <header4.h> ... #endif
Now I solved this problem by adding an extra line to the cpp file
#include <unistd.h> //new added line #include "ah" namespace MyNameSpace { void File::func(int fd) { ::close( fd ); } }
But I'm not satisfied with this solution, since the unistd.h header was already included in ah, but inside the MyAddedNameSpace namespace, or should I add the MyNameSpace prefix to all function calls, where the compiler complains that such a function is not declared?
Thanks for the answer.
source share