LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Compilation Errors in using extern "C" with C++ for DLLs

Hello All,

 

I am trying to compile a fully functional existing source code to a DLL so that I can integrate it with LabView (using Call Library Function Node).

This link provides all the info required    http://decibel.ni.com/content/docs/DOC-1690

 

However, my problem is when I add  extern "C" __declspec( dllexport ) .......  (as per above link)    the code does not compile - gives me errors.

It compiles fine without the extern.....

 

I have simplified my code (hence very crude) to highlight the problem and attached it hereI would appreciate someone can tell me why I cannot compile it when I include extern "C"...

Thanks in advance. 

 

 

#include "stdafx.h"

#include <vector>

#include <math.h>

using namespace std;

const double kPI = 3.14159265358979323846;

 

//extern "C" __declspec( dllexport ) vector<double> deconWave1();

vector<double> deconWave1()

{

    vector<
double> originalData( 32, 0.0 );    vector<double> originalFrequencies;

    originalFrequencies.push_back( 0.3 * 2.0 * kPI / originalData.size() );

    double originalAmplitudes[] = { 3.0 };

    double originalPhases[] = { 0.0 };         for ( size_t i = 0; i < originalData.size(); i++ )

        {

            originalData[ i ] = originalAmplitudes[ 0 ] * cos( originalFrequencies[ 0 ] * i + originalPhases[ 0 ] );

        }

return originalData;

}

 

 

 

0 Kudos
Message 1 of 5
(4,675 Views)

extern "C"  is a C++ extension; a plain ANSI C compiler (like CVI) won't know what it means. It tells a C++ compiler not to mangle the function names it puts into the dll. Third party header files normally have a test to avoid this error, but it is perfectly safe simply to delete it for CVI.

 

JR

0 Kudos
Message 2 of 5
(4,670 Views)

Hello JR,

Thank you for the reply.  I forgot to mention in my post that I use Microsoft Visual C++ 2008 Express as the compiler (not CVI). 

Microsoft Visual C++ 2008 Express does not give any errors with extern "C"... when I used it with the NI example code given in http://decibel.ni.com/content/docs/DOC-1690

 

However, when using Microsoft Visual C++ 2008 Express as the compiler, extern "C"... gives errors for my listed code.  (without extern "C" ... my code compiles well)

 

Any help to compile my code with extern "C" using Microsoft Visual C++ 2008 Express would be appreciated
 

Thanks in advance

0 Kudos
Message 3 of 5
(4,646 Views)
I think the problem is that you want to export a "vector <double>" .  How should a C-compiler handle a "vector <double>" ?  It doesn't know templates. 
0 Kudos
Message 4 of 5
(4,637 Views)

completely offtopic, but worth noting: did you consider using a valarray for holding your data ? the <valarray> header defines a lot of mathematical function which applies to a whole array (including trigonometric functions), taking care of the loop to walk through the data. also, a valarray can be efficiently sliced, that is you can create an array which is a subset of another array without overhead.

the valarray class is one of least known container class, although it has many advantages over a vector for frequent and/or global data manipulation.

0 Kudos
Message 5 of 5
(4,627 Views)