在C++中,可以使用一些宏定义来帮助适配不同操作系统的相对路径。以下是一些常用的宏定义:
linux: Linux操作系统的宏定义_WIN32: Windows操作系统的宏定义APPLE: macOS操作系统的宏定义可以根据不同操作系统来设置不同的路径分隔符和根路径,例如:
#ifdef _WIN32 const std::string rootPath = "C:\\Users\\username\\"; const std::string separator = "\\";#else const std::string rootPath = "/home/username/"; const std::string separator = "/";#endif然后在使用相对路径时,可以使用这些宏定义来拼接路径:
std::string filePath = rootPath + "Documents" + separator + "file.txt";这样就可以根据不同操作系统来适配相对路径了。


