Note that there are some explanatory texts on larger screens.

plurals
  1. POBest code for compiling static const int = X in VS2008 and GCC
    primarykey
    data
    text
    <p>I have run into a problem while writing C++ code that needs to compile in Visual Studio 2008 and in GCC 4.6 (and needs to also compile back to GCC 3.4): <code>static const int</code> class members.</p> <p><a href="https://stackoverflow.com/questions/5391973/undefined-reference-to-static-const-int">Other</a> <a href="https://stackoverflow.com/questions/1312241/using-a-static-const-int-in-a-struct-class">questions</a> <a href="https://stackoverflow.com/questions/5508182/static-const-int-causes-linking-error-undefined-reference">have</a> <a href="https://stackoverflow.com/questions/6138895/weird-linker-problem-with-static-const-class-members">covered</a> the rules required of static const int class members. In particular, the standard and GCC require that the variable have a definition in one and only one object file.</p> <p>However, Visual Studio creates a LNK2005 error when compiling code (in Debug mode) that does include a definition in a .cpp file.</p> <p>Some methods I am trying to decide between are:</p> <ul> <li>Initialize it with a value in the .cpp file, not the header.</li> <li>Use the preprocessor to remove the definition for MSVC.</li> <li>Replace it with an enum.</li> <li>Replace it with a macro.</li> </ul> <p>The last two options are not appealing and I probably won't use either one. The first option is easy -- but I <em>like</em> having the value in the header.</p> <p>What I am looking for in the answers is a good looking, best practice method to structure the code to make both GCC and MSVC happy at the same time. I am hoping for something wonderfully beautiful that I haven't thought of yet.</p>
    singulars
    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.
 

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