How to Compile and Link an External Material Model
To export functions from the DLL when using Microsoft Visual Studio to compile your library, you must declare the functions as __declspec(dllexport). Therefore, to write a source code that works across platforms, use the following #define pattern:
#ifdef _MSC_VER
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif
 
EXPORT <return_type> eval(<arguments>) { }
Compiling and Linking
To compile the function into a library, place it in a file (here called ext.c as an example) and proceed as follows depending on the platform:
See www.comsol.com/system-requirements for information about supported compiler versions.
-
Start Visual Studio 2022 > Visual Studio Tools > VC > x64 Native Tools Command Prompt for VS 2022 from the Windows Start menu.
-
cd to the directory that contains ext.c.
-
cl /MT /c ext.c
-
link /OUT:ext.dll /DLL ext.obj
-
cd to the directory that contains ext.c.
-
icx -fPIC -c ext.c
-
icx -shared -fPIC -o ext.so ext.o -ldl
-
cd to the directory that contains ext.c.
-
gcc -fPIC -c ext.c
-
gcc -shared -fPIC -o ext.so ext.o -ldl
-
cd to the directory that contains ext.c.
-
clang -fPIC -c ext.c
-
clang -shared -fPIC -o ext.dylib ext.o
For other compilers, refer to the compiler’s documentation for instructions on how to compile and create a shared library.
Linking with Utility Functions
To use the provided utility functions in your external material function, you need to reference a header file that contains the utility function declarations. Add the following line to your file (again, called ext.c as an example):
#include "csextutils.h"
Proceed as follows, depending on the platform:
-
Start Visual Studio 2022 > Visual Studio Tools > VC > x64 Native Tools Command Prompt for VS 2022 from the Windows Start menu.
-
cd to the directory that contains ext.c
-
cl /MT /c ext.c /I C:\Program Files\COMSOL\COMSOL64\Multiphysics\data\extmat
-
link /OUT:ext.dll /DLL ext.obj
C:\Program Files\COMSOL\COMSOL64\Multiphysics\data\extmat\win64\csextutils.lib
-
cd to the directory that contains ext.c
-
icx -fPIC -c -I/usr/local/comsol64/multiphysics/data/extmat ext.c
-
icx -shared -fPIC -o ext.so ext.o
-L/usr/local/comsol64/multiphysics/lib/glnxa64 -lcsextutils
-
cd to the directory that contains ext.c
-
gcc -fPIC -c ext.c
-
gcc -shared -fPIC -o ext.so ext.o -ldl
-
cd to the directory that contains ext.c
-
clang -fPIC -c -I/Applications/COMSOL64/Multiphysics/data/extmat ext.c
-
clang -shared -fPIC -o ext.dylib ext.o
-L/Applications/COMSOL64/Multiphysics/lib/maci64 -lcsextutils
For other compilers, refer to the compiler’s documentation for instructions on how to compile and create a shared library.