0

I have a C++ program that has several .hpp files with declarations of variables (most of them paths to a NFS filesystem) and .cpp files with the definitions of those variables.

In some of those variables, whose type is std::string, I need to build its content by appending some content passed as parameter in the main program. For instance:

File constants.hpp:

namespace constants {
    extern std::string cudnn_version;
    extern const std::string path_caffe_cuda;
    extern const std::string path_caffe_cuda_cudnn;
}

File constants.cpp:

const std::string constants::path_caffe_cuda = "/nfs/apps/caffe/cuda";
const std::string constants::path_caffe_cuda_cudnn = constants::path_caffe_cuda + "/cudnn" + constants::cudnn_version;

The content of constants::cudnn_version is asked to the user in the main program as parameter and updated there. The problem is, when constants::path_caffe_cuda_cudnn variable must be built with the content of constants::cudnn_version variable, its content is still empty, so in some way the variable path_caffe_cuda_cudnn is evaluated before the constants::cudnn_version has the content passed by user.

How do you think I could fix the issue?

Thank you very much to everybody.

Tony D.
  • 13
  • 2
  • 1
    Instead of having it be a variable, make it a function which constructs and returns the path? – Some programmer dude Jun 15 '18 at 06:23
  • see https://stackoverflow.com/questions/3035422/static-initialization-order-fiasco – Alan Birtles Jun 15 '18 at 06:28
  • Well, that was an approach I thoght (I have some other examples working like this), but in this case I cannot avoid the cudnn_version variable to be passed as argument in the main program... Would it be possible to contruct the path with one of the parts being passed as argument and the other parts being catched from constants.cpp? Again, thanks a lot. – Tony D. Jun 15 '18 at 06:38

1 Answers1

1

It could easily be done by using a function instead:

namespace constants {
    extern std::string cudnn_version;
    extern const std::string path_caffe_cuda;

    inline std::string path_caffe_cuda_cudnn()
    {
        return constants::path_caffe_cuda + "/cudnn" + constants::cudnn_version;
    }
}

As long as path_caffe_cuda_cudnn is not called until constants::cudnn_version have been initialized, then it will be okay.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thank you! Unfortunatelly it doesn't work, at least when calling the function from constants.cpp file, again it seems to be evaluating the content of the variable before constants::cudnn_version has been initialized, and I think I have no control about when its content is being evaluated. Another option is to call this function as many times I need to access the variable, and I guess it could work... – Tony D. Jun 15 '18 at 07:00
  • @TonyD. If both variables have been initialized (that's the important part) then it should work okay. If one or both haven't been initialized then it will of course not work. It seems you need to rethink your design. – Some programmer dude Jun 15 '18 at 07:02
  • Yes, that's the important part and the root of the problem as well ;) Since I need those variables to be defined in that cpp file (because there are more variables that are built with them) I don't actually know how to get a better desing. Again, thanks a lot for your help. – Tony D. Jun 15 '18 at 07:15