Skip to content

Instantly share code, notes, and snippets.

@ryutorion
Created August 21, 2021 13:43
Show Gist options
  • Save ryutorion/24520f6e503cb3ec093eb0820b83be87 to your computer and use it in GitHub Desktop.
Save ryutorion/24520f6e503cb3ec093eb0820b83be87 to your computer and use it in GitHub Desktop.
#if _WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include <Windows.h>
int main(int argc, const char * argv[])
{
char path[MAX_PATH];
GetModuleFileNameA(nullptr, path, sizeof(path));
std::cout << path << std::endl;
}
#else
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/limits.h>
int main()
{
char tmp[PATH_MAX];
auto result = readlink("/proc/self/exe", tmp, PATH_MAX);
tmp[result] = '\0';
printf("%s\n", tmp);
char path[PATH_MAX] {};
realpath(tmp, path);
printf("%s\n", path);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment