• 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 问题反馈可发送邮件到stubbornhuang@qq.com

  • 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

  • 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

  • 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

  • 感谢大家访问本站,希望本站的内容可以帮助到大家!

  • 工资「喂饱肚子」,副业「养活灵魂」!

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

  • 本站会放置Google广告用于维持域名以及网站服务器费用。

C++ – Windows和Linux系统下获取当前可执行程序的绝对路径

C++ 发布于2022-03-22 阅读 6,829次 0次评论 0次点赞 本文共2597个字,阅读需要7分钟。

1 C++获取WindowsLinux系统当前可执行程序的绝对路径

在程序中获取当前程序在系统中的绝对路径是频繁使用的功能,本文总结了如何在Windows和Linux系统获取当前可执行程序绝对路径的方式,并封装成可跨平台编译的工具类PathUtils。

1.1 在Windows系统上获取当前可执行程序的绝对路径

通过调用Windows API GetModuleFileName函数获取当前可执行程序的绝对路径。

#include<Windows.h>
static HMODULE GetSelfModuleHandle()
{
    MEMORY_BASIC_INFORMATION mbi;
    return (
        (::VirtualQuery(GetSelfModuleHandle, &mbi, sizeof(mbi)) != 0)
        ? (HMODULE)mbi.AllocationBase : NULL
        );
}

std::string PathUtils::GetCurrentExeDirectory()
{
    std::string strCurrentPath = "";
    char curDirector[260] = { 0 };
    GetModuleFileName(GetSelfModuleHandle(), curDirector, 260);
    strCurrentPath = curDirector;

    size_t nameStart = strCurrentPath.rfind("\\");
    strCurrentPath = strCurrentPath.substr(0, nameStart + 1);
    return strCurrentPath;
}

1.2 在Linux系统上获取当前可执行程序的绝对路径

通过readlink函数获取当前可执行程序的绝对路径。

#include <string.h>
#include <unistd.h>
#include <dlfcn.h>

std::string PathUtils::GetCurrentExeDirectory()
{
    std::string strCurrentPath = "";
    char szCurWorkPath[256];
    memset(szCurWorkPath,'\0',256);
    int nRet = readlink ("/proc/self/exe", szCurWorkPath , 256);
    if(nRet>256||nRet<0)
    {
        return strCurrentPath;
    }

    for(int i=nRet;i>0;i--)
    {
        if(szCurWorkPath[i]=='/' || szCurWorkPath[i]=='\\')
        {
            szCurWorkPath[i]='\0';
            break;
        }
    }

    strCurrentPath = szCurWorkPath;

    return strCurrentPath;
}

1.3 PathUtils工具类

PathUtils.h

#ifndef PATH_UTILS_H
#define PATH_UTILS_H

#include <string>

class PathUtils
{
public:
    // 得到当前的Exe的路径
    static std::string GetCurrentExeDirectory();
};


#endif // !PATH_UTILS_H

PathUtils.cpp

#include "PathUtils.h"

#ifdef defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)

#include<Windows.h>

#elif defined(linux) || defined(__linux)

#include <string.h>
#include <unistd.h>
#include <dlfcn.h>

#endif // WINDOWS



#ifdef defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
    static HMODULE GetSelfModuleHandle()
    {
        MEMORY_BASIC_INFORMATION mbi;
        return (
            (::VirtualQuery(GetSelfModuleHandle, &mbi, sizeof(mbi)) != 0)
            ? (HMODULE)mbi.AllocationBase : NULL
            );
    }

    std::string PathUtils::GetCurrentExeDirectory()
    {
        std::string strCurrentPath = "";
        char curDirector[260] = { 0 };
        GetModuleFileName(GetSelfModuleHandle(), curDirector, 260);
        strCurrentPath = curDirector;

        size_t nameStart = strCurrentPath.rfind("\\");
        strCurrentPath = strCurrentPath.substr(0, nameStart + 1);
        return strCurrentPath;
    }
#elif defined(linux) || defined(__linux)
    std::string PathUtils::GetCurrentExeDirectory()
    {
        std::string strCurrentPath = "";
        char szCurWorkPath[256];
        memset(szCurWorkPath,'\0',256);
        int nRet = readlink ("/proc/self/exe", szCurWorkPath , 256);
        if(nRet>256||nRet<0)
        {
            return strCurrentPath;
        }

        for(int i=nRet;i>0;i--)
        {
            if(szCurWorkPath[i]=='/' || szCurWorkPath[i]=='\\')
            {
                szCurWorkPath[i]='\0';
                break;
            }
        }

        strCurrentPath = szCurWorkPath;

        return strCurrentPath;
    }
#endif

在linux系统上编译时需要链接dl选项,

target_link_libraries(main dl rt)

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:C++ – Windows和Linux系统下获取当前可执行程序的绝对路径

原文链接:https://www.stubbornhuang.com/2051/

发布于:2022年03月22日 16:52:27

修改于:2023年06月26日 20:25:45

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
OpenCV - linux上编译出现undefined reference to `cv::VideoCapture::VideoCapture()'错误
OpenCV
下一篇
FFmpeg - ./configure编译参数全部总结和整理
FFmpeg
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号