Developer Guide and Reference

Contents

Creating Libraries

Libraries are simply an indexed collection of object files that are included as needed in a linked program. Combining object files into a library makes it easy to distribute your code without disclosing the source. It also reduces the number of command-line entries needed to compile your project.

Static Libraries

Executables generated using static libraries are no different than executables generated from individual source or object files. Static libraries are not required at runtime, so you do not need to include them when you distribute your executable. At compile time, linking to a static library is generally faster than linking to individual source files.
To build a static library on Linux*:
  1. Use the
    c
    option to generate object files from the source files:
    icpc -c my_source1.cpp my_source2.cpp my_source3.cpp
  2. Use the GNU* tool
    ar
    to create the library file from the object files:
    ar rc my_lib.a my_source1.o my_source2.o my_source3.o
  3. Compile and link your project with your new library:
    icpc main.cpp my_lib.a
If your library file and source files are in different directories, use the
L
dir
option to indicate where your library is located:
icpc -L/cpp/libs main.cpp my_lib.a
To build a static library on
macOS*
:
  1. Use the following command to generate object files and create the library file:
    icpc -fpic -o mylib.a -staticlib my_source1.cpp my_source2.cpp my_source3.cpp
  2. Compile and link your project with your new library:
    icpc main.cpp my_lib.a
If your library file and source files are in different directories, use the
L
dir
dir
option to indicate where your library is located:
icpc -L/cpp/libs main.cpp my_lib.a
If you are using Interprocedural Optimization, see the topic on Creating a Library from IPO Objects using
xiar
.

Shared Libraries

Shared libraries, also referred to as dynamic libraries or Dynamic Shared Objects (DSO), are linked differently than static libraries. At compile time, the linker insures that all the necessary symbols are either linked into the executable, or can be linked at runtime from the shared library. Executables compiled from shared libraries are smaller, but the shared libraries must be included with the executable to function correctly. When multiple programs use the same shared library, only one copy of the library is required in memory.
To build a shared library on Linux*:
  1. Use options
    fPIC
    and
    c
    to generate object files from the source files:
    icpc -fPIC -c my_source1.cpp my_source2.cpp my_source3.cpp
  2. Use the
    shared
    option to create the library file from the object files:
    icpc -shared -o my_lib.so my_source1.o my_source2.o my_source3.o
  3. Compile and link your project with your new library:
    icpc main.cpp my_lib.so
To build a shared library on
macOS*
:
  1. Use the following command to generate object files and create the library file:
    icpc -fPIC -o my_lib.so -dynamiclib my_source1.cpp my_source2.cpp my_source3.cpp
  2. Compile and link your project with your new library:
    icpc main.cpp my_lib.dylib
Use the following options to create libraries on Windows*:
Option
Description
/LD
,
/LDd
Produces a DLL.
d
indicates debug version.
/MD
,
/MDd
Compiles and links with the dynamic, multi-thread C run time library.
d
indicates debug version.
/MT
,
/MTd
Compiles and links with the static, multi-thread C run time library.
d
indicates debug version.
/Zl
Disables embedding default libraries in object files.

Product and Performance Information

1

Intel's compilers may or may not optimize to the same degree for non-Intel microprocessors for optimizations that are not unique to Intel microprocessors. These optimizations include SSE2, SSE3, and SSSE3 instruction sets and other optimizations. Intel does not guarantee the availability, functionality, or effectiveness of any optimization on microprocessors not manufactured by Intel. Microprocessor-dependent optimizations in this product are intended for use with Intel microprocessors. Certain optimizations not specific to Intel microarchitecture are reserved for Intel microprocessors. Please refer to the applicable product User and Reference Guides for more information regarding the specific instruction sets covered by this notice.

Notice revision #20110804