Extern C

This is a discussion on Extern C within the C Programming forums, part of the General Programming Boards category; I am making a .dll where do i add the extern C command? here is the prototype definitions of me ...

  1. #1
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168

    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?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    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);
    Last edited by chico1st; 06-12-2008 at 01:53 PM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    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.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    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'

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    string is not a C keyword. Are you trying to use it in a C function?

  9. #9
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    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.

  10. #10
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    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
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern classes
    By Swordsman in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2008, 02:07 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. Odd Problem with Linking
    By jamez05 in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 01:49 PM
  4. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 10:11 PM
  5. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 02:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21