Reputation: 53
In Windows you can do:
CSystemInfo info;
this->m_strVersion = info.GetFileVersion( CFileSystemHelper::GetApplicationPath() + _T("/test.exe") );
to get the version number.
How would I do it in C++ on linux ?
Upvotes: 3
Views: 3176
Reputation: 12866
Windows adopts a version resource system with standard API support, Linux and UNIX have no such high level concepts for a variety of reasons ranging from legacy to redundancy.
Best options are to query the local packaging system (RPM, APT, etc), or try executing with --version
command line parameter which is a recommended GNU standard.
Example RPM query on command line for the Samba tool smbget
:
# rpm -q -f /usr/bin/smbget --queryformat '%{version}\n' 3.0.33
Upvotes: 3
Reputation: 1
You probably want to retrieve the path of the currently executing executable.
On Linux, you could use the /proc/
pseudo-file system. Read the proc(5) man page for more.
Specifically, you probably want to do something like
char myexepath[512];
memset (myexepath, 0, sizeof(myexepath);
readlink ("/proc/self/exe", myexepath, sizeof(myexepath));
(but you really should check for runtime errors above)
If you simply wanted to display the version of a program, you should have a convention about it. Usually accepting --version
as the program first argument.
I invite you to read Advanced Linux Programming.
Upvotes: 2