Mac: How to export characters from an executable?

I am writing an executable file that uses dlopen () (LoadLibrary () on Windows) to dynamically load a shared library. The shared library uses characters from an executable file.

On Windows, this is possible. Executable files can export symbols: declspec (dllexport) and .def files work. The compiler, when creating the .exe, also creates the .lib file (the "import library"), so the DLL just needs to be associated with this .lib.

On Linux, this is possible. I pass -Wl, -export_dynamic when creating the executable so that it exports its characters.

On Mac OS X instead ... -Wl -export_dynamic does not work, but there is -Wl, -exported_symbols_list, <filename> , where <filename> is a list of exported characters (something like a simpler version of the .def file). But then creating a shared library is not so simple: the linker complains about unresolved characters.

I tried to hack: renamed the executable to lib <executable> .dylib and, linking the shared library, I passed -l <executable> . But it gives the error "cannot communicate with the main executable file."

A common problem is that there may be unresolved characters in shared Linux libraries, while Windows and Mac OS X do not allow this. But Windows has “import libraries” for resolving characters against dependencies, and Mac OS X does not seem to ...

How can this be solved on Mac OS X? Is there an equivalent to an “import library” (a stub library created by the Windows linker when creating the .dll, so if any module should dynamically reference the .dll, is it associated with the “import library”)? Or some other solution?

+6
linker ld macos shared
source share
2 answers

The standalone Lua interpreter supports the dynamic loading (via dlopen) of shared libraries that use characters from the executable (Lua API). When creating it, a special link flag is not used. Shared libraries are created using this spell:

 env MACOSX_DEPLOYMENT_TARGET=10.3 gcc -bundle -undefined dynamic_lookup -o random.so lrandom.o 
+5
source share

Thank you, your answer has stimulated the desire to explore the difference between bundles and dilibs. The ld man page mentions the -bundle_loader option

-bundle_loader executable This indicates the executable that will load the bundled associated output file. Undefined characters from the set are checked for the specified executable file, for example, one of the dynamic libraries with which the package was associated.

(note that when building dylib -bundle_loader does not work, it only works with packages) So, the old command line

 cc -shared -o <output>.so <input>.c 

turned into

 cc -bundle -bundle_loader <executable> -o <output>.so <input>.c 

and the output node resolved its Undefined characters against the executable.

+4
source share

All Articles