Extern functions calls

This is a discussion on Extern functions calls within the C Programming forums, part of the General Programming Boards category; Hi everyone, i'm using dev c++ 4.9.9.2. I have an application wich is compose by four files: main.c TIFFheader.c TIFFheader.h ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    48

    Extern functions calls

    Hi everyone, i'm using dev c++ 4.9.9.2. I have an application wich is compose by four files:

    main.c
    TIFFheader.c
    TIFFheader.h
    decs.h

    In main.c file, i want to call some functions wich are located in TIFFheader.c. Also i've defined a user data type in TIFFheader.h that i want to use from main.c archive. For doing it, i include TIFFheader.h in main.c. In TIFFheader.c, i need to include TIFFheader.h too, because in this file i declare some useful constants, structures and all functions wich belong to TIFFheader.c. When i do that, i keep getting the error constants and structures were defined twice. So, how can i do to call functions, use structures and constants, from main.c and do it from TIFFheader.c too, without this error?
    Regards!.

  2. #2
    Registered User starcatcher's Avatar
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    45

    Post

    Can you give some context? Perhaps even attach the files so we can have a look at what is going on.
    Philipp
    I program solely as a hobby so this is definitely not homework.
    Best thing that's been said to me:
    I was so pleasantly surprised to see another post in this thread, and yours was brilliant; a fitting end to a subject (matrix manipulations of all kinds) that is quite intriguing.
    Read this thread to find out why...
    Cannibalism

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    Sounds like you need include guards.

    http://en.wikipedia.org/wiki/Include_guard

    gg

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    48
    Wow that was quick, thaks guys for your responses. I try codeplug's solution, but it just don't work. Here is what i'm trying to do, and also necessary file's code structure to help you understand me in a better manner.

    TIFFheader.h
    Code:
    #ifndef  hfile_TIFFheader
    #define hfile_TIFFheader
    
    typedef struct s_tiff_header
    {
        short lsb;
        long bitsPerPixel;
        long imageWidth;
        long imageHeight;
        long stripOffset;
    }tiff_header;
    
    ... // constant declarations come here
    
    ... // declarations of  functions located at TIFFheader.c file
    
    #endif
    Decs.h
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <io.h>
    #include <fcntl.h>
    #include <dos.h>
    #include <math.h>
    #include <malloc.h>
    #include <string.h>
    #include <sys\types.h>
    #include <sys\stat.h>
    #include "TIFFheader.h"
    
    ... // unions declared here
    TIFFheader.c
    Code:
    #include "decs.h"             // relevant line           
    
    tiff_header getTiffHeader ( const char* filename )
    {
        tiff_header header;
        char buffer[12];
        FILE *image;
        
        short notFinished,
              tag,
              tagValueType,
              directoriesCount;
        
        int i, 
            bytesRead;
    
        ... // function goes on, and others too
    main.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "decs.h"           //relevant include
    
    int main(int argc, char *argv[])
    {
      tiff_header hdr;
      char filepath[20];
      
      printf ( "Ingrese la ruta de la imagen: \n" );
      scanf ( "%s" , filepath );
      printf ( "Usted ingreso %s", filepath );
      hdr = getTiffHeader ( filepath ); 
      system("PAUSE");	
      return 0;
    }
    The specific error is
    multiple definition of __BYTE
    multiple definition of __ASCII
    multiple definition of __SHORT
    multiple definition of __LONG
    multiple definition of __RATIONAL
    ...

    and so on with all constants declared below. So, it tells to me that i'm defined constant, but not functions and structures twice.
    Am i applying codeplug's solution well?. I hope you can help me out on this guys.
    Thanks in advance.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Are the constants extern'ed in the header files?

    And are those your constants in the warnings?

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    48
    No and yes, are the responses to your questions. Here is the declaration of the constants

    Code:
    /**********************************
    * Tipo del valor del tag
    ***********************************/
    
    const short __BYTE = 1;
    const short __ASCII = 2;
    const short __SHORT = 3;
    const short __LONG = 4;
    const short __RATIONAL = 5;
    
    /**********************************
    * Comienzo estructuras de 
    * información dentro del tag
    **********************************/
    
    const int __TAGVALUEOFFSET = 8;
    const int __TAGVALUETYPEOFFSET = 2;
    const int __TAGVALUETYPELENGHTOFFSET = 4;
    const int __TAGCLASSOFFSET = 0;

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I believe your header file should have "extern const int X", and then "const int X = value" in ONE of your C files.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User starcatcher's Avatar
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    45
    I don't know how it is with other compilers, but with mine you can do something like 'const int x=4;'. Here is a snippet of 'global.h' which I include successfully into all the other files in the same project.
    Code:
    // Global.h
    #ifndef GLOBAL_H
    #define GLOBAL_H
    ...
    // Pieces
    const int EE=0;
    const int WP=1;
    const int WN=2;
    const int WB=3;
    const int WR=4;
    const int WQ=5;
    const int WK=6;
    ...
    #endif
    // EOF
    With extern however, as matsp pointed out, you cannot do the same.
    I program solely as a hobby so this is definitely not homework.
    Best thing that's been said to me:
    I was so pleasantly surprised to see another post in this thread, and yours was brilliant; a fitting end to a subject (matrix manipulations of all kinds) that is quite intriguing.
    Read this thread to find out why...
    Cannibalism

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    48
    I did what matsp said. If i don't assigned a value to the constant at moment i declare it, errors dissapear, but when i try to give a value to the constant in one of the .c files (TIFFheader.c exactly speaking) errors appear again. So, now the trouble is how to assign the value to constant. I really appreciate how you are helping me, thanks guys.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you, perhaps, forget the "extern" that you also need in the header-file?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    48
    I forgot to tell you, but not to write it in my code, here is exactly what i'm doing (besides of taking aspirins for my headacke, wich is obviously provoked by this error):

    TIFFheader.h
    Code:
    ...
    
    /**********************************
    * Tipo del valor del tag
    ***********************************/
    
    extern const short __BYTE;
    extern const short __ASCII;
    extern const short __SHORT;
    extern const short __LONG;
    extern const short __RATIONAL;
    
    /**********************************
    * Comienzo estructuras de 
    * informaci&#243;n dentro del tag
    **********************************/
    
    extern const int __TAGVALUEOFFSET;
    extern const int __TAGVALUETYPEOFFSET;
    extern const int __TAGVALUETYPELENGTHOFFSET;
    extern const int __TAGCLASSOFFSET; 
    
    /* Fin declaraci&#243;n constantes */
    
    ...

    TIFFheader.c
    Code:
    #include "decs.h"
    
    const short __BYTE = 1;
    const short __ASCII = 2;
    const short __SHORT = 3;
    const short __LONG = 4;
    const short __RATIONAL = 5;
    
    const int __TAGVALUEOFFSET = 8;
    const int __TAGVALUETYPEOFFSET = 2;
    const int __TAGVALUETYPELENGTHOFFSET = 4;
    const int __TAGCLASSOFFSET = 0;
    
    ...
    Last edited by mariano_donati; 02-14-2008 at 06:40 AM.

  12. #12
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    That looks correct. What are the errors you're getting now?

    gg

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    By the way, C99 states: "All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use."

    Consequently, it would be better to not to define __BYTE, __ASCII, __ETC.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    48
    The error was the same, but i renamed consts, and it worked well!. Thank you all for helping me out. I'm really, really greateful.
    Regards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 12:00 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. Extern functions?
    By Petike in forum C++ Programming
    Replies: 8
    Last Post: 08-06-2008, 12:33 PM
  4. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

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