Looking for a solution?

Where there's a problem there's a solution

  • FLUENT 6 - How to add C++ object files in UDF

  • The procedure to add pre-compiled object files in UDFs is given in the UDF manual but it requires a few more instructions in order to add C++ functions in the UDF. Here is the procedure to add C++ functions in the UDF on Linux and Windows platforms.
    On Linux:

    1. Create .o file using the command: gcc -fpic -shared -ansi -Wall -O -c cppfile.cpp
    2. The names of functions get changed in C++, which is known as "mangling." Use the following command to determine the new names:
    nm cppfile.o
    3. Now, define these functions in the UDF file as:
    extern int add_two_numbers (int x, int y);
    extern Z15add_two_numbersii (int x, int y);
    #define add_two_numbers Z15add_two_numbersii
    4. Compile the UDF and copy the .o file to libudf/lnx86/2d directory. Edit the makefile in order to add .o file under the USER_OBJECTS.
    5. Issue "make" command in the libudf/lnx86/2d directory and load the libudf in FLUENT.

    On Windows:

    To create the object files on Windows use the following command:
    cl /c /Za test.cpp

    To handle the mngling in C++, one needs to issue the command - DUMPBIN/SYMBOLS test.obj like we did for the Linux platform.
    Unfortunately, Windows mangled names have illegal charachters in them such as : ?@. So writing general link code is not straightforward. A better way is to make an interface file that provides a C interface. This file is in C++ but uses the extern "C" directive to ensure the function name is not mangled. Note that the interface names cannot be the same as any of the C++ functions. Also note that global variables do not get mangled (unlike functions). However, it is still best to use get_ and set_ functions if available.

    Let's assume one has to add two C++ files (test.cpp, SimpleFraction.cpp) in the UDF. Here is the procedure for the same:

    1. Create the C interface files (test_C_Interface.cpp and SimpleFraction_C_Interface.cpp) for each CPP file.
    2. Compile the UDF (with the headers files if needed). While compiling it gives some error for the object files but ignore these errors.
    3. Now, create the four object files from these cpp files and copy these .o files to libudfntx863d directory.
    4. A file called user_nt.udf exists under libudfntx863d directory. Add these objects files in the user_nt.udf file as : USER_OBJECTS = test.obj test_C_Interface.obj SimpleFraction.obj SimpleFraction_C_Interface.obj
    5. Issue the "nmake" command under the same directory.
    6. Now, load the libudf in FLUENT and it should work.



  • Show Form
    No comments yet. Be the first to add a comment!