After you complete the myshared.c file, build the library project in an external IDE.
The process of building a library project is specific to each integrated development environment (IDE) and each operating system. You can use the following compiler/platform combinations to build shared libraries to use in LabVIEW:
You can set up a project that compiles myshared.c and generates myshared.dll.
Adding the DLL Export Keyword
You must explicitly export each function from your DLL to make it available to LabVIEW. For this example, use the _declspec (dllexport) keyword to export the avg_num function. _declspec (dllexport) is a Microsoft-specific extension to the C or C++ language. By declaring the dllexport keyword, you eliminate the need for a module definition file.
Open myshared.c and insert the _declspec(dllexport) keyword in front of the code for avg_num. This function also has a declaration statement, and you must place the keyword in front of the declaration, too.
The following excerpt shows the two places in myshared.c that require the _declspec(dllexport) keyword:
_declspec(dllexport) long avg_num(float a[], long size, float *avg);
_declspec(dllexport) long avg_num(float a, long size, float *avg)
Setting Up the Project Using Microsoft Visual C++ 6.0
You can set up your project in the Microsoft Visual C++ 6.0 integrated development environment. Complete the following steps to set up a project for myshared.c.
- Select File»New to display the New window.
- Select Win32 Dynamic Link Library and enter the name and location information for the DLL.
 |
Note This example does not use Microsoft Foundation Classes (MFC). However, if you want to use these object classes in a project, you can select MFC AppWizard (dll) at this point instead of selecting Win32 Dynamic-Link Library. Then, copy the code from the myshared.c source file and place it into the skeleton source code file that the MFC AppWizard generates.
|
- Click the OK button to display the Win32 Dynamic-Link Library-Step 1 of 1 window.
- Select An empty DLL project in this window.
- Click the Finish button to display the New Project Information window. This window contains summary information for your project.
- Click the OK button to finish creating your project and return to the Visual C++ workspace.
- Add the source file to the project.
- Select Project»Add to Project»Files to display a file dialog box.
- Navigate to the myshared.c source file.
- Click the OK button to add the source file to the project.
 |
Note If you want to use the LabVIEW manager functions in a Windows DLL, you also must add labviewv.lib to your project. The cintools directory of the LabVIEW installation contains this .lib file. If you build a CIN, you also must select cin.obj, labviewv.lib, lvsb.lib, and lvsbmain.def from the cintools directory. |
- Select Project»Settings to display the Project Settings dialog box.
- Click the C/C++ tab of the Project Settings dialog box and configure the following settings:
- Select All Configurations in the Settings For drop-down list.
- Select Code Generation from the Category drop-down list.
- Set the Struct member alignment control to 1 Byte.
- You now can configure the settings for the Release build of the project. Select Win32 Release in the Settings For drop-down list.
- Select Code Generation from the Category drop-down list.
- Select Multithreaded DLL from the Use run-time library drop-down list.
- You now can configure the settings for the Debug build of the project. Select Win32 Debug in the Settings For drop-down list.
- Select Code Generation from the Category drop-down list.
- Select Debug Multithreaded DLL from the Use run-time library drop-down list.
- Click OK to save the project settings.
- If you build a CIN, you also must complete the following steps:
- Select Preprocessor from the Category drop-down list.
- Add the path to the cintools directory in the Additional include directories field.
- Click the Custom Build tab.
- Set the Commands field to <your path to cintools>\lvsbutil "$(TargetName)" -d "$(WkspDir)\$(OutDir)"
- Set the Outputs field to $(OutDir)$(TargetName).lsb. You now can configure the settings for the Release build of the project.
- Build the DLL.
- Select the desired configuration to build from the Build»Set Active Configuration window - either Win32 Debug or Win32 Release.
- Select Build»Build myshared.dll. This will cause Visual C++ to build the DLL and place it in either the Debug or Release output directory, depending on which active configuration you selected.
You then can call this DLL from LabVIEW.
Setting Up the Project using Microsoft Visual C++ .NET 2003
You can set up your project in the Microsoft Visual C++ .NET 2003 integrated development environment. Complete the following steps to set up a project for myshared.c.
- Select File»New»Project to display the New Project window.
- From the Project Types list, open the Visual C++ Projects folder and select Win32.
- From the Templates list, select Win32 Project and enter the name and location information for the DLL.
 |
Note This example does not use Microsoft Foundation Classes (MFC). However, if you want to use these object classes in a project, you can select MFC and MFC DLL at this point instead of selecting Win32 and Win32 Project. Then, copy the code from the myshared.c source file and place it into the skeleton source code file that the MFC AppWizard generates. |
- Click the OK button to display the Win32 Application Wizard window.
- On the Application Settings page, select DLL as the Application type and Empty project in the Additional options section.
- Click the Finish button to finish creating your project and return to the Visual C++ workspace. The project appears as an empty project.
- Add the source file to the project.
- Select Project»Add Existing Item to display a file dialog box.
- Navigate to the myshared.c source file.
- Click the Open button to add the source file to the project. If you want to use the LabVIEW manager functions in a Windows DLL, you also must add labviewv.lib to your project. The cintools directory of the LabVIEW installation contains this .lib file. If you build a CIN, you also must select cin.obj, labviewv.lib, lvsb.lib, and lvsbmain.def from the cintools directory.
- Select Project»Properties to display the Property Pages window and configure the following settings:
- Select All Configurations in the Configuration drop-down list.
- Select General from the C/C++ folder.
- Add the path to the cintools directory to the Additional Include Directories control.
- Select Code Generation from the C/C++ folder.
- Set the Struct Member Alignment control to 1 Byte.
- Select General from the Custom Build Step folder.
- Set the Command Line control to <cintools directory>\lvsbutil "$(TargetName)" -d "$(ProjectDir)$(OutDir)"
- Set the Outputs control to $(OutDir)\$(TargetName).lsb.
- Click Apply to save your changes. You now can configure the settings for the Release build of the project.
- Select Release in the Configuration drop-down list.
- Select Code Generation from the C/C++ folder.
- Set the Runtime Library control to Multi-threaded DLL (/MD).
- If you build a CIN, you also must complete the following steps:
- Select Input from the Linker folder.
- Add <cintoolsdir>\lvsbmain.def to the Module Definition File control.
- Select General from the Custom Build Step folder.
- Set the Command Line control as follows: <path to cintools>\lvsbutil "$(TargetName)" -d "$(ProjectDir)$(OutDir)"
- Set the Outputs control as follows: $(OutDir)\$(TargetName).lsb
- Click the Apply button to save changes to the Release build of the project.
- You now can configure the settings for the Debug build of the project. Select Debug in the Configuration drop-down list.
- Select Code Generation from the C/C++ folder.
- Set the Runtime Library control to Multi-threaded Debug DLL (/MDd).
- Click OK to save these settings and return to the Visual C++ workspace.
- Build the DLL.
- Select the Solution Configuration you want to build, Debug or Release.
- Select Build»Build MyProject to build the DLL and place it in either the Debug or Release output directory, depending on which Solution Configuration you selected.
Libraries that are dynamically loaded are called frameworks or bundles on Mac. You can call frameworks or bundles from a Call Library Function Node. In the Call Library Function dialog box, if you enter foo.* in the Library Name or Path field, LabVIEW searches for foo.framework.
You must include the -malign-natural setting in the Other C Compiler Flags section of the GCC Compiler Settings. If you want to call functions in LabVIEW from the framework, you need to link against liblvexports.a, located in cintools/Mach-O.
Refer to the KnowledgeBase for more information about creating a framework on Mac.
Use the following command to compile the myshared.c source file that you completed:
gcc -fPIC -shared -o <output name> <source file>
The –fPIC option instructs GCC to produce position-independent code, which is suitable for shared libraries. The -shared option specifies that the output should be a shared library file.
 |
Note Some versions of the Gnu linker do not produce suitable output for shared libraries. The –fno-gnu-linker instructs GCC to use the system linker rather than the Gnu linker. The output name is normally a file with a .so extension on Linux. |
Reducing Symbol Scope
By default, all symbols, such as functions and global variables, that are defined in your code are available. Use a mapfile to distinguish between those symbols that external objects can access and those for internal use only. A mapfile is a text document that the linker takes as input and uses to determine, among other things, which symbols to export.
Use the following basic syntax for a mapfile, where <library file> is the name of the output file:
<library file> {
global:
[Symbol for global scope 1];
[Symbol for global scope 2];
...
local:
[Symbols for local scope 1]; or *
...
};
Under the global and local sections, list all of the symbols that you want to be available globally or locally, respectively. Each section is optional, but all symbols are global by default. In the local section, you can choose to use the * wildcard rather than listing individual symbols. This wildcard means "any symbol not already defined as global" and allows you to make symbol definitions easily in terms of symbols to be exported rather than in terms of symbols to be reduced in scope.
After you create the mapfile, save it and instruct the linker to use it by appending -M <mapfile> to the gcc command-line argument list.