Search code examples
c++boostpathfilesystems

Get absolute path with boost::filesystem::path


My current working directory is located at /home/myuser/program, I created a boost::filesystem::path object pointing to it. I appended /../somedir so it becomes /home/myuser/program/../somedir. But I need to get its resolved absolute path, which would be /home/myuser/somedir.

I have been trying for long time and I do not find any method in their reference to do this. There is a method called make_absolute, which seems to be supposed to do what I expect, but I have to give it a “root” path argument. Which should it be? do I really need to do this to get the real absolute path? Is there any other way?


Solution

  • You say you want an absolute path, but your example shows that you already have an absolute path. The process of removing the .. components of a path is known as canonicalization. For that, you should call canonical. It happens to also perform the task of absolute, so you don't need to call absolute or make_absolute first. The make_absolute function requires a base path; you can pass it current_path() if you don't have anything better.