-
Extern C
I am making a .dll
where do i add the extern C command?
here is the prototype definitions of me source code, do i add it here?:
Code:
void DisplayIMAQError(int32_t error);
int AnalogueCardSnap(uint32_t Sid, uint8_t* ImaqBuffer, uint32_t top, uint32_t left, uint32_t height, uint32_t width);
int AnalogueCardInit (uint32_t *Sid, uint32_t *Iid, uint8_t *image1D, uint8_t **image2D);
int AnalogueCardClose(uint32_t *Sid, uint32_t *Iid);
Here is my header file, do i add it here?:
Code:
#include "niimaq.h"
// Error display function
__declspec(dllexport) void DisplayIMAQError(int32_t error);
//
// Callbacks
__declspec(dllexport) int AnalogueCardInit(unsigned int32_t *Sid, unsigned int32_t *Iid, int *width, int *height,unsigned int8_t *image1D,unsigned int8_t **image2D);
__declspec(dllexport) int AnalogueCardClose(unsigned int32_t *Sid, unsigned int32_t *Iid);
__declspec(dllexport) int AnalogueCardSnap(unsigned int32_t Sid,unsigned int8_t *ImaqBuffer, unsigned int32_t top, unsigned int32_t left, unsigned int32_t height, unsigned int32_t width);
there is also my definitions file should i add it there?
-
Typically,
Code:
extern "C"
{
/* My functions */
}
Note that you only need extern "C" if you intend for C++ to call those functions (otherwise you will get a linking error).
I'm assuming the code is compiled in C.
-
but do i do that in the definition file or the source code or both?
I am writing the code in C and calling it in MATLAB.
So would it be like:
Code:
#include "niimaq.h"
#include "stdint.h"
// Error display function
extern "C" __declspec(dllexport) void DisplayIMAQError(int32_t error);
//
// Callbacks
extern "C" __declspec(dllexport) int AnalogueCardInit(uint32_t *Sid, uint32_t *Iid, uint8_t *image1D, uint8_t **image2D);
extern "C" __declspec(dllexport) int AnalogueCardClose(uint32_t *Sid, uint32_t *Iid);
extern "C" __declspec(dllexport) int AnalogueCardSnap(uint32_t Sid, uint8_t *ImaqBuffer, uint32_t top, uint32_t left, uint32_t height, uint32_t width);
Code:
#include <stdio.h>
#include "HLGrab.h"
#include <memory.h>
#include <stdlib.h>
#include "stdint.h"
#include "niimaq.h"
// error checking macro
#define errChk(fCall) if (error = (fCall), error < 0) {goto Error;} else
//prototypes
void DisplayIMAQError(int32_t error);
int AnalogueCardSnap(uint32_t Sid, uint8_t* ImaqBuffer, uint32_t top, uint32_t left, uint32_t height, uint32_t width);
int AnalogueCardInit (uint32_t *Sid, uint32_t *Iid, uint8_t *image1D, uint8_t **image2D);
int AnalogueCardClose(uint32_t *Sid, uint32_t *Iid);
-
Then you don't need extern "C" at all (your functions are already being compiled as exported as C). Unless MATLAB requires some special indication for telling it that it's C, you won't need it.
But if you're curious, you only need it on the declarations.
-
i know matlab needs it for C++ dll's and i currently cannot input my library into matlab so I thought this might do the trick.
-
I assume they are going to include that header file then?
Just put an extern "C" block:
Code:
extern "C"
{
void myfunc1();
void myfunc2();
}
etc.
That should do the trick.
-
I changed some things around in my :
Code:
#include "niimaq.h"
// Error display function
extern "C" __declspec(dllexport) void DisplayIMAQError(Int32 error);
// Callbacks
extern "C" __declspec(dllexport) int AnalogueCardInit(SESSION_ID *Sid, INTERFACE_ID *Iid);
extern "C" __declspec(dllexport) int AnalogueCardClose(SESSION_ID *Sid, INTERFACE_ID *Iid);
extern "C" __declspec(dllexport) int AnalogueCardSnap(SESSION_ID Sid, uInt8 *ImaqBuffer);
and now i get this error... does anyone know what it means?
Code:
1>c:\documents and settings\esorense\desktop\imaq code\1.0\1.0.1dll\101Test.h(4) : error C2059: syntax error : 'string'
-
string is not a C keyword. Are you trying to use it in a C function?
-
I get that same error if the source file that the function is in is just a .c
If you change it to .cpp, you should be ok.
-
The extern "C" construct does not exist in C! That's why the compiler is complaining, it sees a string (the "C") and barfs.
You'll need to do the following:
Code:
#ifdef __cplusplus
extern "C" {
#endif
/* All your code that needs C linkage when compiled with C++ */
#ifdef __cplusplus
}
#endif
This will then ensure that you only use the extern "C" when you're compiling the file in C++.
QuantumPete