Intel® oneAPI DPC++/C++ Compiler Developer Guide and Reference

ID 767253
Date 6/24/2024
Public
Document Table of Contents

Create Libraries

Libraries are an indexed collection of object files, which 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 and reduces the number of command-line entries needed to compile your project.

To create libraries, use the lib.exe tool or xilib.exe tool.

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 distributing your executable. Linking to a static library can be more efficient at compile time than linking to individual source files.

The following steps show you how to build a static library.

Linux

  1. Use the c option to generate object files from the source files:

    icpx -c my_source1.cpp my_source2.cpp my_source3.cpp
  2. Create the library file from the object files. With the GNU* tool ar:

    ar rc my_lib.a my_source1.o my_source2.o my_source3.o

    If using the flto option during the compile step, you must use the LLVM tool llvm-ar to create the library file from the object files:

    llvm-ar rc my_lib.a my_source1.o my_source2.o my_source3.o
  3. Compile and link your project with your new library:

    icpx main.cpp my_lib.a

If your library file and source files are in different directories, use the Ldir option to indicate where your library is located:

icpx -L/cpp/libs main.cpp my_lib.a

Windows

  1. Use the c option to generate object files from the source files:

    icx -c my_source1.cpp my_source2.cpp my_source3.cpp
  2. Create the library file from the object files with the lib command:

    lib -out:my_lib.lib my_source1.obj my_source2.obj my_source3.obj

    If using the flto option during the compile step, you must use the LLVM tool llvm-lib to create the library file from the object files:

    llvm-lib -out:my_lib.lib my_source1.obj my_source2.obj my_source3.obj
  3. Compile and link your project with your new library:

    icx main.cpp my_lib.lib

If your library file and source files are in different directories, use the Ldir option to indicate where your library is located:

icx -L/cpp/libs main.cpp my_lib.lib

Shared Libraries

Shared libraries, also called dynamic libraries or Dynamic Shared Objects (DSO), are linked differently than static libraries. At compile time, the linker ensures 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 small, 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.

Linux

These steps show how to build a shared library on Linux.

  1. Use options fPIC and c to generate object files from the source files:

    icpx -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:

    icpx -shared -o my_lib.so my_source1.o my_source2.o my_source3.o
  3. Compile and link your project with your new library:

    icpx main.cpp my_lib.so

Windows

Use the following options to create libraries on Windows:

Option

Description

/LD, /LDd

Produces a DLL. d indicates the debug version.

/MD, /MDd

Compiles and links with the dynamic, multi-thread C runtime library. d indicates the debug version.

/MT, /MTd

Compiles and links with the static, multi-thread C runtime library. d indicates the debug version.

/Zl

Disables embedding default libraries in object files.

See Also