C++ Linking

From Schmid.wiki
Jump to: navigation, search

Contents

C++ Linking with Visual Studio

Static libraries (.lib) can be linked into a DLL without any issues.

DLL's require an import library (also .lib) to use (or possibly some other import specification).

Creating a DLL is done in these steps:

  • Declare exports "dllexport"
  • Compile as DLL, which creates a import .lib for linking with other stuff, and the actual DLL

You can inspect the exported symbols using dumpbin /all FILE. To use Visual Studio command line tools, execute:

"%vcinstalldir%\bin\vcvars32.bat"

Exporting All Public Symbols

GCC exports all public symbols to a shared library automatically. According to MSDN, Visual Studio or link.exe cannot automatically export symbols, you have to specify them manually, either by prefixing all methods with __declspec(dllexport) or by manually generating a .def file.

As of 2010-04-13, I have not been able to find any tool to do this automatically.

C++ Linking on Mac OS X

On Mac OS X, libraries have the following naming standard:

libstaticlibrary.a
libsharedlibrary.dylib

Loading shared libraries is case-insensitive.

If .dylibs cannot be located, use:

DYLD_LIBRARY_PATH=/path/to/lib/ ./executable 

Creating Shared Library from Static Libraries

g++ -dynamiclib -all_load libinput1.a libinput2.a -o liboutput.dylib

-all_load is a ld inherited by g++ which forces ld to include all members of the static archive libraries in the output shared library.

Checking the output:

nm liboutput.dylib
Personal tools