By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,906 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,906 IT Pros & Developers. It's quick & easy.

Problem with gcc build - #including files

P: 31
I have the following files:


netfunc.h:

Expand|Select|Wrap|Line Numbers
  1. typedef struct{
  2.      char data[MAX];
  3.      int n;
  4. }packet;
  5.  
  6. typedef struct
  7. {
  8.      char flag_s;
  9.      char address;
  10.      unsigned char control;
  11.      char data[MAX];
  12.      unsigned char FCS1;
  13.      unsigned char FCS2;
  14.      char flag_e;
  15. }frame;
  16.  
  17. typedef struct{
  18.     frame trama;
  19.     packet paquete;
  20.     FILE* file;
  21.     int tty;
  22. }arguments;
  23.  
  24.  
  25. void to_physical_layer(frame *fr, int fds);
  26. void from_physical_layer(frame *fr, int fde);
  27. void print_frame(frame trama);
  28. void format_frame(frame *fs, packet ps,int control);
  29. void format_packet(frame fs, packet *ps);
  30. void to_network_layer (packet pr, FILE *pfs);
  31.  

TX_automat_SW.h

Expand|Select|Wrap|Line Numbers
  1. /* ----------------------------------------------------------- *
  2.  * Author: Luis Sieira García <email removed>
  3.  * Date: May 2007
  4.  * No rights reserved
  5.  * ----------------------------------------------------------- */
  6. #include <IFTI_P2_automat.h>
  7.  
  8. #define    REACHED_EOF        3
  9.  
  10.  
  11.  
  12. /* ------------------------------------------------------------ 
  13.  * State definitions
  14.  * ------------------------------------------------------------ */
  15. enum {
  16.     TX_STATE_INITIAL = AUTOMAT_INITIAL_STATE,
  17.     TX_STATE_READING,
  18.     TX_STATE_FRAMING,
  19.     TX_STATE_WAITING
  20. };
  21.  
  22. /* ------------------------------------------------------------ 
  23.  * Event definitions
  24.  * ------------------------------------------------------------ */
  25. enum {
  26.     TX_EV_OPERATIVE = 1,
  27.     TX_EV_READOK,
  28.     TX_EV_FRAMED,
  29.     TX_EV_TWOFRAMES,
  30.     TX_EV_ACK_RECEIVED,
  31. };
  32.  
  33. /* ------------------------------------------------------------ 
  34.  * Action definitions
  35.  * ------------------------------------------------------------ */
  36. TAutomat * TX_automat_SW_define( );
  37.  
  38. int read_data(    TEntity * inEntity,
  39.             TState inFromState,
  40.             TEvent inEvent,
  41.             TEventArgs * inArgs );
  42.  
  43. int manage_data(    TEntity * inEntity,
  44.             TState inFromState,
  45.             TEvent inEvent,
  46.             TEventArgs * inArgs );
  47.  
  48. int send_data_wait_response(    TEntity * inEntity,
  49.             TState inFromState,
  50.             TEvent inEvent,
  51.             TEventArgs * inArgs );
  52.  

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...
May 31 '07 #1
Share this Question
Share on Google+
6 Replies


RedSon
Expert 5K+
P: 5,000
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_frame" 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?
May 31 '07 #2

P: 31
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_frame" 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:

Expand|Select|Wrap|Line Numbers
  1.      #include "TX_automat_SW.h"
  2.      #include "netfunc.h"
  3.  
  4.      #include <unistd.h>
  5.      #include <stdio.h>
  6.      #include <stdlib.h>
  7.      #include <string.h>
  8.  
When I run my make.

Expand|Select|Wrap|Line Numbers
  1. sieira@GATTACA:/DOCS/Mis documentos/Estudios pendientes/Teleinformatica/LABORATORIO/Practica II/Programas$ make
  2. gcc -g -c -Wall -I ./include src/IFTI_P2_automat.c -o src/IFTI_P2_automat.o
  3. gcc -g -c -Wall -I ./include src/TX_automat_SW.c -o src/TX_automat_SW.o
  4. gcc -g -c -Wall -I ./include src/TX_SW.c -o src/TX_SW.o
  5. src/TX_SW.c: In function ‘main’:
  6. src/TX_SW.c:26: error: ‘arguments’ undeclared (first use in this function)
  7. src/TX_SW.c:26: error: (Each undeclared identifier is reported only once
  8. src/TX_SW.c:26: error: for each function it appears in.)
  9. src/TX_SW.c:26: error: expected ‘;’ before ‘args’
  10. src/TX_SW.c:31: error: ‘frame’ undeclared (first use in this function)
  11. src/TX_SW.c:31: error: expected ‘;’ before ‘inFrame’
  12. src/TX_SW.c:32: error: ‘packet’ undeclared (first use in this function)
  13. src/TX_SW.c:32: error: expected ‘;’ before ‘inPacket’
  14. src/TX_SW.c:41: error: ‘MAX’ undeclared (first use in this function)
  15. src/TX_SW.c:41: error: ‘args’ undeclared (first use in this function)
  16. src/TX_SW.c:88: warning: implicit declaration of function ‘print_frame’
  17. src/TX_SW.c:92: warning: implicit declaration of function ‘from_physical_layer’
  18. src/TX_SW.c:92: error: ‘inFrame’ undeclared (first use in this function)
  19. src/TX_SW.c:93: error: ‘CONTROL’ undeclared (first use in this function)
  20. src/TX_SW.c:94: error: ‘ACK’ undeclared (first use in this function)
  21. src/TX_SW.c:96: warning: implicit declaration of function ‘format_packet’
  22. src/TX_SW.c:96: error: ‘inPacket’ undeclared (first use in this function)
  23. make: *** [src/TX_SW.o] Error 1
  24.  
(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:

Expand|Select|Wrap|Line Numbers
  1. sieira@GATTACA:/DOCS/Mis documentos/Estudios pendientes/Teleinformatica/LABORATORIO/Practica II/Programas$ make
  2. gcc -g -c -Wall -I ./include src/IFTI_P2_automat.c -o src/IFTI_P2_automat.o
  3. gcc -g -c -Wall -I ./include src/TX_automat_SW.c -o src/TX_automat_SW.o
  4. gcc -g -c -Wall -I ./include src/TX_SW.c -o src/TX_SW.o
  5. gcc  ./src/IFTI_P2_automat.o ./src/TX_automat_SW.o ./src/TX_SW.o -o bin/TX_SW
  6. ./src/TX_automat_SW.o: In function `manage_data':
  7. src/TX_automat_SW.c:81: referencia a `format_frame' sin definir
  8. ./src/TX_automat_SW.o: In function `send_data_wait_response':
  9. src/TX_automat_SW.c:95: referencia a `to_physical_layer' sin definir
  10. ./src/TX_SW.o: In function `main':
  11. src/TX_SW.c:89: referencia a `print_frame' sin definir
  12. src/TX_SW.c:93: referencia a `from_physical_layer' sin definir
  13. src/TX_SW.c:96: referencia a `print_frame' sin definir
  14. src/TX_SW.c:97: referencia a `format_packet' sin definir
  15. src/TX_SW.c:110: referencia a `print_frame' sin definir
  16. src/TX_SW.c:114: referencia a `from_physical_layer' sin definir
  17. src/TX_SW.c:117: referencia a `print_frame' sin definir
  18. src/TX_SW.c:118: referencia a `format_packet' sin definir
  19. collect2: ld returned 1 exit status
  20. make: *** [bin/TX_SW] Error 1
  21.  
Now types get defined, and no error related is shown...but netfunc.h functions become undefined in both files...

I'm getting crazy...
May 31 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
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:

Expand|Select|Wrap|Line Numbers
  1. #define MAX 100
  2. #define AUTOMAT_INITIAL_STATE  100
  3. class TAutomat
  4. {
  5.  
  6. };
  7. class TEntity
  8. {
  9.  
  10. };
  11. class TState
  12. {
  13.  
  14. };
  15. class TEvent
  16. {
  17.  
  18. };
  19. class TEventArgs
  20. {
  21.  
  22. };
  23.  
just before the netfunc.h. I just made up values to see if it would compile.

My code looks like:

Expand|Select|Wrap|Line Numbers
  1. #define MAX 100
  2. #define AUTOMAT_INITIAL_STATE  100
  3. class TAutomat
  4. {
  5.  
  6. };
  7. class TEntity
  8. {
  9.  
  10. };
  11. class TState
  12. {
  13.  
  14. };
  15. class TEvent
  16. {
  17.  
  18. };
  19. class TEventArgs
  20. {
  21.  
  22. };
  23.         ///////////////////////////////////////////
  24.         //netfunc.h
  25.         typedef struct{
  26.      char data[MAX];
  27.      int n;
  28. }packet;
  29.  
  30. typedef struct
  31. {
  32.      char flag_s;
  33.      char address;
  34.      unsigned char control;
  35.      char data[MAX];
  36.      unsigned char FCS1;
  37.      unsigned char FCS2;
  38.      char flag_e;
  39. }frame;
  40.  
  41. typedef struct{
  42.     frame trama;
  43.     packet paquete;
  44.     FILE* file;
  45.     int tty;
  46. }arguments;
  47.  
  48.  
  49. void to_physical_layer(frame *fr, int fds);
  50. void from_physical_layer(frame *fr, int fde);
  51. void print_frame(frame trama);
  52. void format_frame(frame *fs, packet ps,int control);
  53. void format_packet(frame fs, packet *ps);
  54. void to_network_layer (packet pr, FILE *pfs);
  55.  
  56. ///////////////////////////////
  57. int main()
  58. {
  59.  
  60.  
  61.         ///////////////////////////////////////////
  62. /* ----------------------------------------------------------- *
  63. * Author: Luis Sieira García <email removed>
  64. * Date: May 2007
  65. * No rights reserved
  66. * ----------------------------------------------------------- */
  67. //#include <IFTI_P2_automat.h>
  68.  
  69. #define REACHED_EOF    3
  70.  
  71.  
  72.  
  73. /* ------------------------------------------------------------ 
  74. * State definitions
  75. * ------------------------------------------------------------ */
  76. enum {
  77.     TX_STATE_INITIAL = AUTOMAT_INITIAL_STATE,
  78.     TX_STATE_READING,
  79.     TX_STATE_FRAMING,
  80.     TX_STATE_WAITING
  81. };
  82.  
  83. /* ------------------------------------------------------------ 
  84. * Event definitions
  85. * ------------------------------------------------------------ */
  86. enum {
  87.     TX_EV_OPERATIVE = 1,
  88.     TX_EV_READOK,
  89.     TX_EV_FRAMED,
  90.     TX_EV_TWOFRAMES,
  91.     TX_EV_ACK_RECEIVED,
  92. };
  93.  
  94. /* ------------------------------------------------------------ 
  95. * Action definitions
  96. * ------------------------------------------------------------ */
  97. TAutomat * TX_automat_SW_define( );
  98.  
  99. int read_data(  TEntity * inEntity,
  100.             TState inFromState,
  101.             TEvent inEvent,
  102.             TEventArgs * inArgs );
  103.  
  104. int manage_data(    TEntity * inEntity,
  105.             TState inFromState,
  106.             TEvent inEvent,
  107.             TEventArgs * inArgs );
  108.  
  109. int send_data_wait_response(    TEntity * inEntity,
  110.             TState inFromState,
  111.             TEvent inEvent,
  112.             TEventArgs * inArgs );
  113.  
  114.  
  115.         /////////////////////////////////////////////////
  116.  
  117. }
May 31 '07 #4

P: 31
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

Expand|Select|Wrap|Line Numbers
  1. CC=gcc
  2. CXX=gcc
  3. INCLUDES=-I ./include
  4. CFLAGS=-g -c -Wall
  5. LDFLAGS=
  6. SOURCES=./src/IFTI_P2_automat.c ./src/TX_automat_SW.c ./src/TX_SW.c
  7. OBJECTS=$(SOURCES:.c=.o)
  8. EXECUTABLE1=./bin/TX_SW
  9. EXECUTABLE2=./bin/RX_SW
  10.  
  11. all: $(SOURCES) $(EXECUTABLE1) $(EXECUTABLE2)
  12.  
  13. $(EXECUTABLE1): $(OBJECTS) 
  14.     $(CC) $(LDFLAGS) $(OBJECTS) -o $@
  15.  
  16. $(EXECUTABLE2): $(OBJECTS) 
  17.     $(CC) $(LDFLAGS) $(OBJECTS) -o $@
  18.  
  19. .c.o:
  20.     $(CC) $(CFLAGS) $(INCLUDES) $< -o $@
  21.  
  22. clean:
  23.     \rm -rf $(OBJECTS) $(EXECUTABLE1) $(EXECUTABLE2)
  24.  
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
May 31 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
You gotta trade that thing in for an IDE that does the makefiles for you.
May 31 '07 #6

Banfa
Expert Mod 5K+
P: 8,916
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.
May 31 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.