Compiling a UDF

To compile your UDF, you must specify appropriate settings for your compiler.

  1. Specify that the compiler should generate a shared object file (.so) or a .DLL file.
  2. Ensure your list of "include" directories includes the directory that holds c8udf.hc8udf.h.

    If you are on Microsoft Windows and are using Microsoft's Visual Studio, please do the following:

  3. Start Microsoft Visual Studio.
  4. Create a project file by going to the menu and clicking on File -> New -> Project.
    1. Specify that this is a Visual C++ Project.
    2. Click on "Win32".
    3. In the right-hand pane, Click on "Win32 Project".
    4. Fill in the name that you'd like to use for your project.
    5. Browse and specify the directory in which you want the project to be stored.
    6. Click on "OK".
    7. The next window to appear is the "Win32 Application Wizard" window. On the left, click on "Application Settings", then click on "DLL".
    8. Click on "Finish".
  5. Microsoft Visual Studio creates a simple .cpp file to use as a starting point. We recommend that you remove all the contents of this file and insert your own C code for the UDF. Make sure that your code includes the following:
    #include "c8udf.h" 
    	// Ensure functions are "exported" properly from dll. 
    	#if defined(_MSC_VER) 
    	#define USER_FUNCTION_EXPORT __declspec( dllexport ) 
    	#else // defined(_MSC_VER) 
    	#define USER_FUNCTION_EXPORT 
    	#endif // defined(_MSC_VER) 
    	extern "C" { 
    	USER_FUNCTION_EXPORT void my_function(C8Udf* ctx); 
    	}; // extern "C"             
    

    You will need to customize the names and return types of each UDF and any intermediate function, if you have one.

  6. If you have other C-language source files that you need, add them to the project.
  7. You will need to update several settings that are available in the "Property Pages" for this project.
    1. Update the list of directories to search for include files.

      To do this, go to the menu, click on "Project" and then on "Properties". You should get a new window titled something like MySample Property Pages.

      In the left-hand pane of this window, click on "Configuration Properties", then "", and then on "General".

      The right-hand pane should now show a list of settings that you can modify.

      Click in the field to the right of Additional Include Directories and add the directory that contains the file c8udf.h file which is included with the Sybase CEP product.

      On Microsoft Windows, this directory is typically:

      C:\Program Files\SybaseC8\Server\sdk\c\include

      On 64-bit Microsoft Windows, this directory is typically:

      C:\Program Files (x86)\SybaseC8\Server\sdk\c\include

      You can also add other directories if necessary for your UDF.

    2. Turn off precompiled headers.

      To do this, go to the left-hand pane in the Property Pages window, click on "" and then on Precompiled headers, then click on Create/Use Precompiled Header and set it to Not Using Precompiled Headers.

    3. Add the Sybase CEP library directory which contains accessor functions that let you read and write information to the context variable to the list of library directories. To do this, go to the left-hand pane of the Property Pages window, click on "Linker" and then on "General".

      In the field to the right of "Additional Library Directories", add the directory that contains the library.

      On 32-bit Microsoft Windows, this directory is typically:

      C:\Program Files\SybaseC8\Server\sdk\c\lib

      On Microsoft Windows, this directory is typically:

      C:\Program Files (x86)\SybaseC8\Server\sdk\c\lib

    4. Tell the linker not to include debugging information. To do this, go to the left-hand pane of the Property Pages window, click on "Linker" and then on "Debugging". For the field "Generate Debugging Info", change the value to "No".
    5. Add a dependency on the c8_sdk_server_lib.lib file. To do this, go to the left-hand pane of the Property Pages window, click on "Linker" and then on "Input".

      In the field to the right of "Additional Dependencies", enter

      c8_sdk_server_lib.lib

      You do not need to enter the complete path; entering the file name is sufficient.

      To double check that you haven't skipped a step, you can look at the "Command Line" for the C/C++ compiler and the Command Line for the Linker. This shows the command-line parameters passed from Microsoft's GUI IDE to the command-line compiler and linker.

      To view the command line for the compiler, go to the left-hand pane of the Property Pages window, click on "C/C++" and then click on "Command Line". The command line should look similar to the following:

      /Od /I "C:\Program Files\SybaseC8\Server\sdk\c\include" 
        /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_USRDLL" 
        /D "TEST_UDF_AGGR1_EXPORTS" /D "_UNICODE" 
        /D "UNICODE" /D "_WINDLL" /Gm /EHsc /RTC1 /MDd 
        /Fo"Debug\\" /Fd"Debug\vc80.pdb" /W3 /nologo /c 
        /Wp64 /ZI /TP /errorReport:prompt
      

      If you set the warning level to a value other than 3, the "/W3" will be different.

      The command line may or may not include:

      /D "_DEBUG"
      

      If the command line includes this, you may only be able to use the .DLL on a computer that has the debug version of the C runtime library. (For more information, see the Troubleshooting section below.)

      To view the command line for the linker, go to the left-hand pane of the Property Pages window, click on "Linker" and then click on "Command Line". The command line should look similar to the following:

      /OUT:"C:\c8test\E2\C_SDK\TAggr1\TAggr1\Debug\TAggr1.dll"
        /INCREMENTAL /NOLOGO 
        /LIBPATH:"C:\Program Files\SybaseC8\Server\sdk\c\lib" 
        /DLL /MANIFEST
        /MANIFESTFILE:"Debug\TAggr1.dll.intermediate.manifest"
        /SUBSYSTEM:WINDOWS /MACHINE:X86 /ERRORREPORT:PROMPT
        c8_sdk_server_lib.lib kernel32.lib user32.lib gdi32.lib
        winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib
        oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
      

      Now that you have entered all the project properties, click the "OK" button on the "Property Pages" window. At this point, you should be ready to compile.

  8. To compile, use the appropriate option on the "Build" menu, for example, "Build MySample".
  9. Copy the DLL file to the server's bin directory, and copy the XML (.udf) file to both the Server's and the Studio's plugins directory. On Microsoft Windows, these directories default to:
    C:\Program Files\SybaseC8\Server\bin
    	C:\Program Files\SybaseC8\Server\plugins
    	C:\Program Files\SybaseC8\Studio\plugins
    

    If you are using a command line compiler such as cc, you can use a make file or a script file.

  10. The examples below are based on the cc compiler found on many UNIX-like operating systems.
    1. This example uses a make file:
      TARGET=libc8_udf_regexp_match.so
      		all: $(TARGET) 
      		OBJECTS = regexp_match.o regexpr2.o syntax2.o
      		C8 = /home/henry/sybasec8
      		SDK_PATH = $(C)/server/sdk/c
      		INC = -I$(SDK_PATH)/include -I$(SDK_PATH)/include/stlport
      		C8_LIB = -L$(SDK_PATH)/lib -lc8_sdk_server_lib -lstlport
      		CPPFLAGS = -fPIC -shared
      		%.o : %.cpp
        gcc $(INC) $(CPPFLAGS) $< -o $@
      		$(TARGET) : $(OBJECTS)
        gcc -o $@ $?
      

      You cannot copy this and use it directly. In addition to customizing the file names, you will also have to replace blank spaces with tab characters in appropriate places to satisfy the make program.

      If you are compiling for a x86 architecture, you will probably also want to use the -m64 flag or equivalent.

    2. This example uses a script:
      cc -I${HOME}/sybasec8/server/sdk/c/include \
           -o libmma.so \
           -L${HOME}/sybasec8/server/sdk/c/lib/ \
           -lc8_sdk_server_lib \
           -fPIC \
           -shared \
           mma.cpp
      

      where mma.c is the name of your source code file and libmma,so is the name you'd like to use for the library file.

      -I specifies the directory(s) to be searched for #include files.

      -o specifies the name of the output file. Typically, this is a shared library file.

      -L specifies the directory(s) to search for library files that need to be linked with this one.

      -l specifies the name of the Sybase CEP library to link with to use the functions.

      -fPIC specifies that the compiler should generate Position-Independent Code, or code for dynamic linking.

      -shared specifies that the output will be a shared object library file as opposed to a stand-alone executable program.

      Although on UNIX-like operating systems your library file name will typically be of the form libXYZ.so, your .udf file should specify only "mma" as the library name; do not specify "libmma.so" in the .udf file. The .udf file interpreter will make platform-specific adjustments for the filename extension (.so vs. .dll) and, if necessary, add an initial "lib" prefix.

      If you are compiling for a 64-bit x86 architecture, you will probably also want to use the -m64 flag or equivalent.

  11. Compile the code by executing the script or the make file.
  12. Copy the shared library file to the server's bin directory. For example:

    /home/<userid>/sybasec8/server/bin

  13. Copy the XML (.udf) file to both the Server and Studio's plugins directory:

    /home/<userid>/sybasec8/server/plugins

    /home/<userid>/sybasec8/studio/plugins