c++numerical-methodspragmainclude-guardsnumerical-recipes

Why are there no include guards in Numerical Recipes header files?


novice C++ programmer here.

I'm using Numerical Recipes (V3) source code as part of a larger, modulated C++ project.

While I'll try not to get into the specifics of my problem, I am curious as to why these NR header files do not incorporate any header guards? I'm aware this question is very specific to those who have used this code in the past, but looking at the NR forums they seem quite inactive...

The errors I'm finding with my code that prompted this question are;

error LNK2005: "void __cdecl gaussj(class NRmatrix &)" (?gaussj@@YAXAAV?$NRmatrix@N@@@Z) already defined in Schmidt_V2_(Zeta).obj

error LNK2005: "void __cdecl gaussj(class NRmatrix &,class NRmatrix &)" (?gaussj@@YAXAAV?$NRmatrix@N@@0@Z) already defined in Schmidt_V2_(Zeta).obj


Solution

  • AFAIK there is no good reason. There are a few situations where you might legitimately want not to use an include guard (see this question), but this isn't one of them.

    If you need to include those headers from several places in your project, you must introduce your own guards, like so:

    #include <a_normal_thing>
    #include <another_normal_thing>
    
    #ifndef SPECIAL_NONESENSE_H
    #define SPECIAL_NONESENSE_H
    #include <special_nonsense>
    #endif
    
    // More normal includes...
    

    This is verbose and annoying, but it'll work.

    EDIT: Or nowadays, it's pretty safe to use #pragma once as Donnie suggests in a comment below. This won't work in versions of GCC older than 3.4, but probably you don't need to support that anymore.