[cfe-dev] Implementation of stdbool.h

Chris Lattner clattner at apple.com
Thu Feb 28 22:18:06 PST 2008

On Feb 28, 2008, at 9:57 PM, Chris Lattner wrote:

> On Feb 28, 2008, at 11:53 AM, Keith Bauer wrote:
>>> The best way I know that avoids having to hard code the prefix into
>>> the executable is by making clang search the "../include/clang" path
>>> from its executable.  Is there a good way to do this?
>> On Mac OS X CFBundle knows how to find the absolute path of the
>> executable.  On Linux one can read the symbolic link /proc/self/exe.
>> On Windows if you open an app from the GUI, the CWD is the one
>> containing the executable, but if you run it on the command-line
>> that's presumably not the case... and on other OSes, I don't know of
>> any way other than Gordon's suggestion of searching PATH for argv[0].
> Ok, I'll see what I can do with argv[0].  If this doesn't work on
> windows, hopefully someone working there can help :)

Actually, dladdr seems useful on unix'y systems.  Does this work on  

#include <stdio.h>
#include <dlfcn.h>

int main() {
   Dl_info info;
   int err;
   printf("%d\n", err = dladdr(main, &info));
   if (err == 0)
     printf("%s\n", info.dli_fname);

$ gcc t.c
$ ./a.out

One issue is that this requires reading through symlinks I guess:

$ cd ..
$ ln -s Desktop/a.out something
$ ./something

It would have been nice if that printed /Users/sabre/Desktop/./a.out.


More information about the cfe-dev mailing list