Note that there are some explanatory texts on larger screens.

plurals
  1. PONamespaces in C Headers
    primarykey
    data
    text
    <p>We have a code base where there are .cpp and .h, it is set up to be build with gcc </p> <p>my understanding is that gcc will choose the appropriate compiler for the appropriate files so I believe (and testing has made me almost 100% sure) that our .h files are compiled as c and our .cpp files are compiled as C++.</p> <p>I once tried to change to g++ and got a whole spate of errors, the same is true when I try and use .hpp. </p> <p>So to get around the issue, most of the time I just use extern C++ when I need to (e.g. when including map) </p> <p>However I can't seem to get it right for namespaces. I assume the compiler is complaining about my namespace because it is being compiled as .h? is this correct. My current code looks like (after trying it without the extern):</p> <p>in foo.h</p> <pre><code> extern "C++" { namespace Caching_Logic { class foo { .... } } } </code></pre> <p>in bar.h</p> <pre><code> extern "C++" { using namespace Caching_Logic; //completely defeats the point of namespaces } class bar { .... foo m_foo; ... } </code></pre> <p>Both files are included by .cpp files</p> <p>and the error when compiling is:</p> <p>filethatusesbar.cpp: error: ‘class bar’ has no member named ‘m_foo’</p> <p>bar.h : error: ‘Caching_Logic’ is not a namespace-name</p> <p>bar.h : error: expected namespace-name before ‘;’ token</p> <p>I am considering just giving up on the namespace entirely but I really like to scope my code for this!</p> <p>-----UPDATE------</p> <p>After reading <a href="http://www.network-theory.co.uk/docs/gccintro/gccintro_54.html" rel="nofollow">http://www.network-theory.co.uk/docs/gccintro/gccintro_54.html</a></p> <p>I have a slightly better understanding of what gcc does. originally I read somewhere that gcc compiles in C++ when necessary and in C when it comes across a .c</p> <p>This is true but it would seem that there is then a problem with the linker. As this is compiled as a library it makes sense that i have not seen an issue before. I am guessing that this is related to my scope issue, external linkage?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload