• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: path to dylib
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: path to dylib


  • Subject: Re: path to dylib
  • From: Eric Albert <email@hidden>
  • Date: Thu, 26 Feb 2004 02:03:41 -0800

This is probably safer because it uses APIs. I believe the _dyld* functions are considered SPIs.

#include <stdio.h>
#include <mach-o/dyld.h>
#include <assert.h>

int main(void) {
	NSSymbol symbol;
	NSModule module;

	symbol = NSLookupAndBindSymbol("_printf");
	assert(symbol != NULL);
	module = NSModuleForSymbol(symbol);
	assert(module != NULL);
	printf("Module library name: %s\n", NSLibraryNameForModule(module));
	return 0;
}

-Eric

At 9:00 AM -0500 2/25/04, Dmitry Markman wrote:
thank you very much it works

here is slightly modified code (it compiled for C)

#include <mach-o/dyld.h>
#include <mach-o/ldsyms.h> //defines _mh_dylib_header see man page for
ld

void foo ()
  {
         int i;
         struct mach_header* header = &_mh_dylib_header;
         const char* imagename = 0;
         int cnt = _dyld_image_count();
         for (i = 1; i < cnt; i++) {
             if (_dyld_get_image_header(i) == header)
                 imagename = _dyld_get_image_name(i);//contains absolute
path to dylib
	}
         if(imagename != NULL) printf("dylib path is %s\n",imagename);
//contains
}

thanks again

On Feb 25, 2004, at 2:58 AM, Miklss Fazekas wrote:

 We use a code like this to get the path of the .dylib

 #include <mach-o/ldsyms.h>
 #include <mach-o/dyld.h>

 void foo ()
 {
 	mach_header* header = 0;
     header = _mh_dylib_header; /* use _mh_bundle_header if you linked
 with bundle0.a and not dyld0.a */
 	const char* imagename = 0;
 	int cnt = _dyld_image_count();
     for (int idx1 = 1; idx1 < cnt; idx1++) {
 		if (_dyld_get_image_header(idx1) == header)
 			imagename = _dyld_get_image_name(idx1);
 	}

     /* your absolute path is in imagename */
 }

 On Feb 25, 2004, at 7:01 AM, email@hidden wrote:

 Hi

 is it possible to get path to dylib in runtime from within dylib's
 function?
 for example I have application foo
 dynamic library     foo.dylib

 and function void foo(void) that I store in the foo.dylib

 application foo load foo.dylib and eventually will call method foo

 so I'd like to discover foo.dylib absolute path from within method foo


AFAIK in Windows it's possible via DLLMain and GetModuleFileName



Dmitry Markman
_______________________________________________
xcode-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/xcode-users
Do not post admin requests to the list. They will be ignored.
_______________________________________________
xcode-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/xcode-users
Do not post admin requests to the list. They will be ignored.


References: 
 >Re: path to dylib (From: Miklós Fazekas <email@hidden>)
 >Re: path to dylib (From: Dmitry Markman <email@hidden>)

  • Prev by Date: Re: Viewing data
  • Next by Date: Re: can't get shell tool target to link against dylib target in same project
  • Previous by thread: Re: path to dylib
  • Next by thread: Handy tip for separate build location
  • Index(es):
    • Date
    • Thread