# Build External Libraries

Use external libraries with OptiStruct for the following applications:
1. External Optimization Responses (DRESP3 Bulk Data Entry)

For responses which are not directly available via DRESP1 or DRESP2 entries, you can create your own responses in Fortran/C and build corresponding shared libraries for OptiStruct to use during the solution. Refer to External Responses for additional information.

2. User-defined Structural Materials (MATUSR Bulk Data Entry)

For materials which are not directly available via MAT# entries, and/or for additional flexibility in the material definition, you can create your own materials using Fortran/C/C++ and build corresponding shared libraries for OptiStruct to use during the solution. Refer to User-Defined Structural Material for additional information.

3. User-defined Thermal Materials (MATUSHT Bulk Data Entry

For thermal materials which are not directly available via MAT# entries, and/or for additional flexibility in the material definition, you can create your own materials using Fortran/C/C++ and build corresponding shared libraries for OptiStruct to use during the solution. Refer to User-Defined Thermal Material for additional information.

4. User-defined Heat Transfer Properties

The QVOL, PCONV, QBDY1, and SPCD entries can be defined with QVOLLIB, PCONVLIB, QBDYLIB, and SPCDLIB to create user-defined heat transfer properties in Fortran/C/C++ and build corresponding shared libraries for OptiStruct to use during the solution. Refer to User-Defined Heat Transfer Properties for additional information.

The following sections illustrate the process of building external libraries using Fortran/C.

## Windows Systems with Microsoft Visual Studio

Creating dynamic libraries in Windows with Microsoft Visual Studio can be done as follows. First create a new Project and in the New Project window, choose one of the following paths:

For Libraries built with Intel Visual Fortran compiler:

New Project > Intel Visual Fortran (Template) > Library > Dynamic Link Library

For Libraries built with C compiler:

New Project > Visual C++ (Template) > Win32 > Win32 Project
Note:
1. For Fortran libraries, you need to change the argument passing conventions in the project settings. Under the Fortran tab, select the category "External Procedures" and then change the "Argument Passing Conventions" to "C, By Reference".
2. On Windows systems, %PATH% must be set correctly to ensure that the right compiler DLLs are picked up at runtime.

## Linux Systems

On Linux systems, the general syntax to build a shared library starting from a Fortran or C file is:
• Create object files using either Fortran or C, as:

For Fortran: FC [options] -c myfile.F -o myfile.o

For C: CC [options] -c myfile.c -o myfile.o

• Create Shared Libraries, as:

LD -shared [options] myfile.o -o mylib.so

Where,
FC
The Fortran compiler (for instance ifc)
CC
The C compiler (for instance cc or gcc)
LD
The linker (for instance ld) installed on your computer

The compiler and linker options provide information about the platform you are building the library for. OptiStruct is a 64-bit application and therefore, only works with 64-bit shared libraries or DLLs (the corresponding options to build 64-bit shared libraries should be used). The linker option “-shared” specifies that you are building a shared library (when using either Intel or GNU compilers). Other options, such as code optimization parameters, are left to your discretion and should not usually affect the compatibility with OptiStruct.

The following table defines options for each of OptiStruct's release platforms, which have been verified to work correctly on various systems. Keep in mind that these options might change depending on the compilers and linker installed on your computer. Refer to your operating system manual for further information. In most cases, GNU compilers can be used in place of Intel compilers. Use the appropriate compiler and linker options to create a shared library with the compiler of your choice.

## Export Subroutines/Functions to Shared Libraries

The following examples illustrate how the defined subroutines/functions are exported to the Shared Libraries. Shared libraries are .dll file in Windows and .so file in Linux.
• Export from Fortran:

!DEC$ATTRIBUTES DLLEXPORT :: <subroutine name> The !DEC$ ATTRIBUTES DLLEXPORT command should be used to export the subroutine to the shared library.

• Export from C:

__declspec(dllexport) int <subroutine name> (<arguments>)

The __declspec(dllexport) command should be used to export the function to the shared library.

Compiler Versions

You can generally use Native C compilers on both Linux (gcc) and Windows (cl.exe), or Intel C compilers (icc) can be used to create shared libraries in C. If using Fortran, then Intel Fortran Compilers can be used to create shared libraries in Fortran. The Compiler version numbers should typically be chosen within the minor releases of the corresponding compiler versions used to build OptiStruct.

To determine this, you can run the OptiStruct script or Compute Console (ACC) GUI with the -buildinfo run option (the Use Solver Control check box should be unchecked on the GUI) to obtain information about the compilers used to build OptiStruct executables and use similar compiler versions to build the shared libraries. This information is under the header “Build Information” in the screen output after the -buildinfo run option is used.

Compiler Options
The compilers in the following table are the ones used to build OptiStruct. The following options can be used during the creation of shared libraries.
Platform Fortran Compiler Options C Compiler Options Linker Options
Win64 /iface:default /libs:dll /threads /MT /LD
Linux64 -fPIC -fPIC -shared
Once your library is built, you can verify that the functions have been exported correctly by using nm mylib.so on Linux systems and dumpbin/exports mylib.dll on Windows systems with Microsoft Visual Studio. This command will display the list of symbols found in the library, among which you should recognize the function(s) which you have written.
Note: Some Fortran compilers convert function names to lower-case or upper-case symbols, and some compilers also append an underscore to these names. However, in your input decks, you do not have to worry about the exact symbol name. Simply use the function name as it is defined in your code, and OptiStruct will automatically locate the appropriate symbol.