Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8748309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:30:12+00:00 2026-06-13T12:30:12+00:00

I am using CMake to compile and test a C++ project. Let’s say I

  • 0

I am using CMake to compile and test a C++ project. Let’s say I have a structure like this:

inc/
    ..
src/
    ..
data/
    dataFile

File dataFile is accessed from code, and will be installed under <prefix>/share or similar. In my code, I do something like:

std::ifstream dataFile(DATAFILE_PATH);

I get that I can configure DATAFILE_PATH with CMake (with header template, or compiling options, or anything) so I don’t have to hardcode the file path in my code directly. Also, I know how to install files to the install tree, and that it is possible to copy files to copy files to build tree, for example for running tests o simply running the executable from build tree.

My question is, how should I configure CMake, or maybe my C++ code, so I can compile my program and run and test it from build tree (accessing a dataFile copy within build tree) and from install tree (accessing the dataFile installed copy under <prefix>/share)? Is there any pattern or technique implemented in CMake that I am missing?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:30:13+00:00Added an answer on June 13, 2026 at 12:30 pm

    I do not know about the CMake particulars about this, so perhaps I’m not giving you the simplest solution. But I can think of two ways of handling this that do not require recompiling the program between the test and the install.

    The first is to not use a single path, but a list of them, and use the first one that can be successfully opened.

    const char* datafile_paths[] = { DATAFILE_PATH };
    ...
    std::ifstream dataFile;
    for(int i = 0; i < sizeof(datafile_paths)/sizeof(char*); i++)
    {
        data.open(datafile_paths[i]);
        if(data.is_open()) break;
    }
    if(!data.is_open()) handle error;
    

    Here DATAFILE_PATH could look like this, for example “/foo/bar/data.dat”, “/foo/data.dat”, i.e. a comma-separated list of paths.

    The other is to use an environment variable instead. For some projects, this might be preferrable as it is more flexible, letting the user choose where to keep the resource files. But it typically means .bashrc or similar needs to be modified, which is a bit icky.

    char * datapath = getenv("MYPROJECT_DATAPATH");
    if(!datapath) handle error;
    std::ifstream data(datapath);
    

    Typically one would not use this technique alone, but combine it with the one above, first checking for the environment variable, and falling back on the compiled-in path list if that fails.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently constructing a project with a plugin structure. I'm using CMake to compile
I am using CMake to build my project and I have the following line:
Currently I use something like this to create project make files: cmake -DCMAKE_INSTALL_PREFIX=./install-dir -DBUILD_WITH_STATIC_CRT=ON
I'm currently using CMake to build my project and CppUnit to Test it. In
I am trying to compile my project with a library called SDL_draw using cmake
I have a number of projects built using CMake and I'd like to be
I'm using android-cmake to compile an application for Android. This essentially creates a CMake
I am using cmake to compile code.In in ,i have used the SHARED option
I am trying to compile c++ project on Redhat Linux 4.1.2 machine using Cmake
I have an c++ application developed using gcc(4.1) and compiled with CMAKE MakeFiles .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.