Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Using External Code in LabVIEW.pdf
Скачиваний:
49
Добавлен:
29.05.2015
Размер:
1.85 Mб
Скачать

Chapter 2 Shared Libraries (DLLs)

11.Right-click the Call Library Function icon and select Create .c file in the shortcut menu. Save the file as myshared.c.

Note In this example, you use a .c source file. When you work with C++ libraries, change the extension of the source file to .cpp.

Preventing C++ Name Decoration

When you build shared libraries for C++, you must prevent the C++ compiler from decorating the function names in the final object code. To do this, wrap the function declaration in an extern "C" clause, as shown in the following prototype.

extern "C" {

long MyDLLFunction(long nInput, unsigned long nOutput, void *arg1);

}

long MyDLLFunction(long nInput, unsigned long nOutput, void *arg1)

{

/* Insert Code Here */

}

Note If you disable C++ decoration of a function, the compiler cannot create polymorphic versions of the function.

Task 2: Complete the .c File

The Call Library Function generates the following source code skeleton in myshared.c:

/* Call Library Source File */ #include "extcode.h"

long avg_num(float a[], long size, float *avg); long avg_num(float a[], long size, float *avg)

{

/* Insert Code Here */

}

Using External Code in LabVIEW

2-8

www.ni.com

Chapter 2 Shared Libraries (DLLs)

Replace the /* Insert Code Here */ spacer with the following function code, making sure to place the code within the pair of curly braces:

int i; float sum=0;

if(a != NULL)

{

for(i=0;i < size; i++) sum = sum + a[i];

}

else

return (1); *avg = sum / size; return (0);

Required Libraries

This simple example requires no header files. When you build more complex shared libraries, you must include header files for all related libraries. For example, a Windows shared library project might need to include windows.h. In another instance, a project might need to include extcode.h, the header file for the set of LabVIEW manager functions that perform simple and complex operations, ranging from low-level byte manipulation to routines for sorting data and managing memory.

When you want to use the LabVIEW manager functions inside your shared library, you must include the LabVIEW library files in your compiled project: labview.lib for Visual C++, labview.sym.lib for Symantec, and labview.export.stub for Metrowerks CodeWarrior. These files appear in the cintools directory of your LabVIEW installation. Specifically, you need the LabVIEW manager functions if you intend to do any of the following:

Allocate, free, or resize arrays, strings, or other data structures that are passed into or out of your library from LabVIEW.

Work with LabVIEW Path data types.

Work with file refnums inside your library.

Use any of the Support Manager functions.

Refer to Chapter 6, Function Descriptions, for more information about the manager functions.

© National Instruments Corporation

2-9

Using External Code in LabVIEW

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]