Problem with gcc build - #including files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sieira
    New Member
    • May 2007
    • 31

    Problem with gcc build - #including files

    I have the following files:


    netfunc.h:

    [CODE=c]typedef struct{
    char data[MAX];
    int n;
    }packet;

    typedef struct
    {
    char flag_s;
    char address;
    unsigned char control;
    char data[MAX];
    unsigned char FCS1;
    unsigned char FCS2;
    char flag_e;
    }frame;

    typedef struct{
    frame trama;
    packet paquete;
    FILE* file;
    int tty;
    }arguments;


    void to_physical_lay er(frame *fr, int fds);
    void from_physical_l ayer(frame *fr, int fde);
    void print_frame(fra me trama);
    void format_frame(fr ame *fs, packet ps,int control);
    void format_packet(f rame fs, packet *ps);
    void to_network_laye r (packet pr, FILE *pfs);
    [/CODE]


    TX_automat_SW.h

    [CODE=c]/* ----------------------------------------------------------- *
    * Author: Luis Sieira García <email removed>
    * Date: May 2007
    * No rights reserved
    * ----------------------------------------------------------- */
    #include <IFTI_P2_automa t.h>

    #define REACHED_EOF 3



    /* ------------------------------------------------------------
    * State definitions
    * ------------------------------------------------------------ */
    enum {
    TX_STATE_INITIA L = AUTOMAT_INITIAL _STATE,
    TX_STATE_READIN G,
    TX_STATE_FRAMIN G,
    TX_STATE_WAITIN G
    };

    /* ------------------------------------------------------------
    * Event definitions
    * ------------------------------------------------------------ */
    enum {
    TX_EV_OPERATIVE = 1,
    TX_EV_READOK,
    TX_EV_FRAMED,
    TX_EV_TWOFRAMES ,
    TX_EV_ACK_RECEI VED,
    };

    /* ------------------------------------------------------------
    * Action definitions
    * ------------------------------------------------------------ */
    TAutomat * TX_automat_SW_d efine( );

    int read_data( TEntity * inEntity,
    TState inFromState,
    TEvent inEvent,
    TEventArgs * inArgs );

    int manage_data( TEntity * inEntity,
    TState inFromState,
    TEvent inEvent,
    TEventArgs * inArgs );

    int send_data_wait_ response( TEntity * inEntity,
    TState inFromState,
    TEvent inEvent,
    TEventArgs * inArgs );
    [/CODE]


    TX_SW

    Manage the automat transitions, so it includes "TX_automat_SW. h".

    It needs to use some functions from netfunc.h

    If I don't include netfunc.h I get

    [...]
    src/TX_SW.c: In function ‘main’:
    src/TX_SW.c:25: error: ‘arguments’ undeclared (first use in this function)
    [...]

    if I include both

    #include <TX_automat_SW. h>
    #include <netfunc.h>

    then functions are not defined in TX_automat_SW

    [...]
    ./src/TX_automat_SW.o : In function `manage_data':
    src/TX_automat_SW.c :80: referencia a `format_frame' sin definir
    [...]


    As I think, solution is related with #ifndef #ifdef and #endif directives, but I've never used it... and I don't know how to do it...
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Originally posted by Sieira
    TX_SW

    Manage the automat transitions, so it includes "TX_automat_SW. h".

    It needs to use some functions from netfunc.h

    If I don't include netfunc.h I get

    [...]
    src/TX_SW.c: In function ‘main’:
    src/TX_SW.c:25: error: ‘arguments’ undeclared (first use in this function)
    [...]

    if I include both

    #include <TX_automat_SW. h>
    #include <netfunc.h>

    then functions are not defined in TX_automat_SW

    [...]
    ./src/TX_automat_SW.o : In function `manage_data':
    src/TX_automat_SW.c :80: referencia a `format_frame' sin definir
    [...]


    As I think, solution is related with #ifndef #ifdef and #endif directives, but I've never used it... and I don't know how to do it...
    First off anything you include that is not part of the standard or platform SDK should be included with double quotes #include "netfunc.h" . Either your compiler will just ignore the difference between <> and "" or it will look in a different path.

    So it looks like in your TX_automat_SW.c you are referencing something called "format_fra me" that is not visitable to TX_automat_SW. It doesnt have anything to do with the #ifdef #endif statements. You are missing a #include in either TX_automat_SW.c or TX_automat_SW.h .

    ¿entiendes?

    Comment

    • Sieira
      New Member
      • May 2007
      • 31

      #3
      Originally posted by RedSon
      First off anything you include that is not part of the standard or platform SDK should be included with double quotes #include "netfunc.h" . Either your compiler will just ignore the difference between <> and "" or it will look in a different path.

      So it looks like in your TX_automat_SW.c you are referencing something called "format_fra me" that is not visitable to TX_automat_SW. It doesnt have anything to do with the #ifdef #endif statements. You are missing a #include in either TX_automat_SW.c or TX_automat_SW.h .

      ¿entiendes?

      Entiendo (o eso creo), gracias... But I couldn't solve my problem

      I've replaced <> with "" around my .h files.

      TX_automat_SW.c includes this:

      [CODE=c]
      #include "TX_automat_SW. h"
      #include "netfunc.h"

      #include <unistd.h>
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      [/CODE]

      When I run my make.

      Code:
      sieira@GATTACA:/DOCS/Mis documentos/Estudios pendientes/Teleinformatica/LABORATORIO/Practica II/Programas$ make
      gcc -g -c -Wall -I ./include src/IFTI_P2_automat.c -o src/IFTI_P2_automat.o
      gcc -g -c -Wall -I ./include src/TX_automat_SW.c -o src/TX_automat_SW.o
      gcc -g -c -Wall -I ./include src/TX_SW.c -o src/TX_SW.o
      src/TX_SW.c: In function ‘main’:
      src/TX_SW.c:26: error: ‘arguments’ undeclared (first use in this function)
      src/TX_SW.c:26: error: (Each undeclared identifier is reported only once
      src/TX_SW.c:26: error: for each function it appears in.)
      src/TX_SW.c:26: error: expected ‘;’ before ‘args’
      src/TX_SW.c:31: error: ‘frame’ undeclared (first use in this function)
      src/TX_SW.c:31: error: expected ‘;’ before ‘inFrame’
      src/TX_SW.c:32: error: ‘packet’ undeclared (first use in this function)
      src/TX_SW.c:32: error: expected ‘;’ before ‘inPacket’
      src/TX_SW.c:41: error: ‘MAX’ undeclared (first use in this function)
      src/TX_SW.c:41: error: ‘args’ undeclared (first use in this function)
      src/TX_SW.c:88: warning: implicit declaration of function ‘print_frame’
      src/TX_SW.c:92: warning: implicit declaration of function ‘from_physical_layer’
      src/TX_SW.c:92: error: ‘inFrame’ undeclared (first use in this function)
      src/TX_SW.c:93: error: ‘CONTROL’ undeclared (first use in this function)
      src/TX_SW.c:94: error: ‘ACK’ undeclared (first use in this function)
      src/TX_SW.c:96: warning: implicit declaration of function ‘format_packet’
      src/TX_SW.c:96: error: ‘inPacket’ undeclared (first use in this function)
      make: *** [src/TX_SW.o] Error 1
      (as you can see "gcc -g -c -Wall -I ./include src/TX_automat_SW.c -o src/TX_automat_SW.o " returns no errors)

      Every error and warning refers to types and functions contained in "netfunc.h" , and called from TX_SW.c so i include it. Since this header is included in both (TX_automat_SW. c and TX_SW.c) files. What I get is this:

      Code:
      sieira@GATTACA:/DOCS/Mis documentos/Estudios pendientes/Teleinformatica/LABORATORIO/Practica II/Programas$ make
      gcc -g -c -Wall -I ./include src/IFTI_P2_automat.c -o src/IFTI_P2_automat.o
      gcc -g -c -Wall -I ./include src/TX_automat_SW.c -o src/TX_automat_SW.o
      gcc -g -c -Wall -I ./include src/TX_SW.c -o src/TX_SW.o
      gcc  ./src/IFTI_P2_automat.o ./src/TX_automat_SW.o ./src/TX_SW.o -o bin/TX_SW
      ./src/TX_automat_SW.o: In function `manage_data':
      src/TX_automat_SW.c:81: referencia a `format_frame' sin definir
      ./src/TX_automat_SW.o: In function `send_data_wait_response':
      src/TX_automat_SW.c:95: referencia a `to_physical_layer' sin definir
      ./src/TX_SW.o: In function `main':
      src/TX_SW.c:89: referencia a `print_frame' sin definir
      src/TX_SW.c:93: referencia a `from_physical_layer' sin definir
      src/TX_SW.c:96: referencia a `print_frame' sin definir
      src/TX_SW.c:97: referencia a `format_packet' sin definir
      src/TX_SW.c:110: referencia a `print_frame' sin definir
      src/TX_SW.c:114: referencia a `from_physical_layer' sin definir
      src/TX_SW.c:117: referencia a `print_frame' sin definir
      src/TX_SW.c:118: referencia a `format_packet' sin definir
      collect2: ld returned 1 exit status
      make: *** [bin/TX_SW] Error 1
      Now types get defined, and no error related is shown...but netfunc.h functions become undefined in both files...

      I'm getting crazy...

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I would be going crazy too. I took your code and compiled it with Visual Studio.NET and got no errors.

        I has to add this:

        [code=cpp]
        #define MAX 100
        #define AUTOMAT_INITIAL _STATE 100
        class TAutomat
        {

        };
        class TEntity
        {

        };
        class TState
        {

        };
        class TEvent
        {

        };
        class TEventArgs
        {

        };
        [/code]

        just before the netfunc.h. I just made up values to see if it would compile.

        My code looks like:

        [code=cpp]
        #define MAX 100
        #define AUTOMAT_INITIAL _STATE 100
        class TAutomat
        {

        };
        class TEntity
        {

        };
        class TState
        {

        };
        class TEvent
        {

        };
        class TEventArgs
        {

        };
        ///////////////////////////////////////////
        //netfunc.h
        typedef struct{
        char data[MAX];
        int n;
        }packet;

        typedef struct
        {
        char flag_s;
        char address;
        unsigned char control;
        char data[MAX];
        unsigned char FCS1;
        unsigned char FCS2;
        char flag_e;
        }frame;

        typedef struct{
        frame trama;
        packet paquete;
        FILE* file;
        int tty;
        }arguments;


        void to_physical_lay er(frame *fr, int fds);
        void from_physical_l ayer(frame *fr, int fde);
        void print_frame(fra me trama);
        void format_frame(fr ame *fs, packet ps,int control);
        void format_packet(f rame fs, packet *ps);
        void to_network_laye r (packet pr, FILE *pfs);

        ///////////////////////////////
        int main()
        {


        ///////////////////////////////////////////
        /* ----------------------------------------------------------- *
        * Author: Luis Sieira García <email removed>
        * Date: May 2007
        * No rights reserved
        * ----------------------------------------------------------- */
        //#include <IFTI_P2_automa t.h>

        #define REACHED_EOF 3



        /* ------------------------------------------------------------
        * State definitions
        * ------------------------------------------------------------ */
        enum {
        TX_STATE_INITIA L = AUTOMAT_INITIAL _STATE,
        TX_STATE_READIN G,
        TX_STATE_FRAMIN G,
        TX_STATE_WAITIN G
        };

        /* ------------------------------------------------------------
        * Event definitions
        * ------------------------------------------------------------ */
        enum {
        TX_EV_OPERATIVE = 1,
        TX_EV_READOK,
        TX_EV_FRAMED,
        TX_EV_TWOFRAMES ,
        TX_EV_ACK_RECEI VED,
        };

        /* ------------------------------------------------------------
        * Action definitions
        * ------------------------------------------------------------ */
        TAutomat * TX_automat_SW_d efine( );

        int read_data( TEntity * inEntity,
        TState inFromState,
        TEvent inEvent,
        TEventArgs * inArgs );

        int manage_data( TEntity * inEntity,
        TState inFromState,
        TEvent inEvent,
        TEventArgs * inArgs );

        int send_data_wait_ response( TEntity * inEntity,
        TState inFromState,
        TEvent inEvent,
        TEventArgs * inArgs );


        /////////////////////////////////////////////////

        }[/code]

        Comment

        • Sieira
          New Member
          • May 2007
          • 31

          #5
          Thank you, I was not getting crazy. I was stupid...

          I wondered "why didn't compile for me if the code has no errors?"

          Obviously the problem was in my Makefile

          Code:
          CC=gcc
          CXX=gcc
          INCLUDES=-I ./include
          CFLAGS=-g -c -Wall
          LDFLAGS=
          SOURCES=./src/IFTI_P2_automat.c ./src/TX_automat_SW.c ./src/TX_SW.c
          OBJECTS=$(SOURCES:.c=.o)
          EXECUTABLE1=./bin/TX_SW
          EXECUTABLE2=./bin/RX_SW
          
          all: $(SOURCES) $(EXECUTABLE1) $(EXECUTABLE2)
          	
          $(EXECUTABLE1): $(OBJECTS) 
          	$(CC) $(LDFLAGS) $(OBJECTS) -o $@
          
          $(EXECUTABLE2): $(OBJECTS) 
          	$(CC) $(LDFLAGS) $(OBJECTS) -o $@
          
          .c.o:
          	$(CC) $(CFLAGS) $(INCLUDES) $< -o $@
          
          clean:
          	\rm -rf $(OBJECTS) $(EXECUTABLE1) $(EXECUTABLE2)
          I forgot to add ./src/netfunc.c in the SOURCES tag!!!

          I don't use to write Makefiles this way... so I didn't see it.

          Thousands of thanks

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            You gotta trade that thing in for an IDE that does the makefiles for you.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by weaknessforcats
              You gotta trade that thing in for an IDE that does the makefiles for you.
              You know that sometimes you just don't get a choice, particularly if you want to do management of multi-platform code.

              Most platforms compilers do not come with an IDE.

              Comment

              Working...