0

I am using a CMake build system for my project.

I want my program executable to know the Project Directory at all times, I thought i could set an environment variable and use:

getenv("MYPROJDIR")

But i don't want to create a persistent variable across the system, and i don't want to manually create a temporary variable everytime before executing my program.

Is there a way to set some variable in build time so that my source knows the project directory, and can access input files relative to that directory regardless of the location where it is executed from.

I could use a define with the -D switch or:

add_definitions(-DMYPROJDIR=${PROJECT_SOURCE_DIR})

but i have heard people say not to use macros unless absolutely necessary.

Overall i need this funtionality:

const string defaultinput = MYPROJDIR + "src/someinputdata.dat"
ifstream myfile(defaultinput)
Vikash Balasubramanian
  • 2,921
  • 3
  • 33
  • 74
  • in your case you can make some configuration file in resources with the different pairs name,value. And place project dir that you want. Another possibility is to pass this path as argument to your application – johngull Dec 29 '16 at 15:23
  • A macro is not such a bad idea here. But if you want more flexibility, you could lookup the executable's path at runtime and load your data file relative to that. This means you can move both to another directory and keep things working. – Olivier Dec 29 '16 at 15:25
  • It is a bad idea indeed. What will you do when your application is deployed or copied to a different machine? How would it know how to access the files on this new machine? – SergeyA Dec 29 '16 at 15:30
  • @SergeyA you are right, but this is a collabarative project and there are some additional default configuration files provided along with the source, which i need to access relative to the project directory, so if i find the project directory the config file will always be there. – Vikash Balasubramanian Dec 29 '16 at 15:33

2 Answers2

2

but i have heard people say not to use macros unless absolutely necessary.

Even if that would be true, I wouldn't say using a -D switch is a macro.

Using -D switches are exactly for this, to add build-time variables to your code. Which is what you seem to want.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • 1
    @Vikash there is no better way to put it. If this is one of the few use cases for which you need this functionality I would go with `-D` as well. However if there are quite a lot of configuration parameters (say 20) then a configuration header configured by CMake will be the better option in the long run. – everclear Dec 29 '16 at 15:32
0

The alternative for defines depend on your use case. The most obvious alternative to build-/compile-time are run-time decisions.

And in your case I don't think putting the absolute path to your program's destination into your program itself is a good idea (see @SergeyA comment).

I would recommend one of the following two run-time solutions:

  1. If this is really data that needs to be part of your application (hard-coded 1:1 relation), then I would probably directly compile the content of src/someinputdata.dat into your program

  2. Get the path of your executable via some program code

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149