0

Build Environment:

  • Visual Studio 2022
  • Windows 10 64-bit
  • C++ Standard : C++20

I am trying to get the directory of my executable file. In my earlier code based on C++17 standard I could read the path using std::filesystem::current_path().string() and store it in a global static std::string object. In my new C++20 project (fist for the standard) I am returned empty string in my global static std::string object, however in the local object I created to store path it is successfully storing executable path.

Source file:

    std::filesystem::path path = std::filesystem::current_path();
    std::string path_string = path.string();    //Working - Path is stored in string variable
    Common::directory_path = path.string();     //Not Working - Empty string string variable
    std::string path_temp = std::filesystem::current_path().string();   //Working - Path is stored in string variable
    Common::directory_path1 = std::filesystem::current_path().string(); //Not Working - Empty string string variable

Header file:

namespace Common 
{   
       static std::string           directory_path{ "" }; 
}

Edit :

I have added my complete code. I have included Common.h in all my other classes (header file for the class) and imported the class header in respective source file.

#include "Common.h"
#include <plog/Appenders/ColorConsoleAppender.h>

bool Common::app_init(void)
{
    try 
    {
        Common::plog_init();
        std::filesystem::path path = std::filesystem::current_path();
        std::string path_string = path.string();    //Working - Path is stored in string variable
        Common::directory_path = path.string();     //Not Working - Empty string string variable
        std::string path_temp = std::filesystem::current_path().string();   //Working - Path is stored in string variable
        Common::directory_path = std::filesystem::current_path().string();  //Not Working - Empty string string variable
        return true;
    }
    catch (const std::exception& ex) 
    {
        LOG_ERROR << ex.what();
    }
    return false;
}

Header:

#pragma once

#include <any>
#include <tuple>
#include <mutex>
#include <array>
#include <queue>
#include <chrono>
#include <string>
#include <vector>
#include <thread>
#include <vector>
#include <memory>
#include <atomic>
#include <variant>
#include <sstream>
#include <optional>
#include <coroutine>
#include <cpr/cpr.h>
#include <algorithm>
#include <filesystem>
#include <functional>
#include <plog/Log.h>
#include <json/json.h>
#include <unordered_map>
#include <pystring/pystring.h>
//#include <sigc++-3.0/sigc++/sigc++.h>
#include <boost/algorithm/string.hpp>


namespace Common
{
    static std::string          directory_path{ "" };
    constexpr   std::size_t     error_file_size = 1024 * 1024 * 100; //100 M.B.
    const       int             max_error_files = 5;
    bool                        app_init(void);
    bool                        plog_init(void);
};

main.cpp

#include "Common.h"

int main()
{
    Common::app_init();

    PLOG_INFO << Common::directory_path;
}
James Z
  • 12,209
  • 10
  • 24
  • 44
Dark Sorrow
  • 1,681
  • 14
  • 37
  • 2
    Sounds like the usual static initialization order fiasco. Would be clearer with a [mre] instead of the pseudo-example snippet. – StoryTeller - Unslander Monica Nov 27 '21 at 13:48
  • Another option is that the header is included in more than one cpp file, and you get a separate static variable in each file. – BoP Nov 27 '21 at 13:57
  • I'm getting kinda lost here: is it supposed be static, i.e. a "per-file" variable, or is it supposed to be shared between files? Because if the header is included in multiple C++ files, you get empty string per file and that's expected. Didn't you mean to make the variable`inline` instead of `static`? – alagner Nov 27 '21 at 15:00
  • @alagner Its needs to be static globally. Separate instance is not created per-flie. Hence I declared it inside namespace and not class. If I remove key-word static it will me multiple-declaration error. – Dark Sorrow Nov 27 '21 at 15:21
  • 1
    @DarkSorrow that's what `inline` is for (since C++17, but `std::filesystem` suggets you're using it already). – alagner Nov 27 '21 at 15:46
  • 1
    @DarkSorrow my point is, the namespace doesn't matter, static that it is, well, [static](https://stackoverflow.com/a/11623473/4885321) i.e. limited to the scope of translation unit. If you want a global to be defined in header, you should use [inline](https://stackoverflow.com/a/47502744/4885321) – alagner Nov 27 '21 at 16:41

0 Answers0