Link to home
Create AccountLog in
Avatar of bass20
bass20

asked on

Newbie library problem

Greetings,

I created a small .h and .c that define and implement a simple data structure, like this:
-->> queue.h
#ifndef QUEUE_H
#define QUEUE_H

#include "comunic.h" //required for type t_pedido
#define QUEUE_LENGTH 16

typedef struct queue {
        t_pedido requests[QUEUE_LENGTH];
        unsigned int currentLength;
} t_queue;

-->> queue.c
//I'll post only the headers of the functions:
void queueInit(t_queue *queue);
unsigned int queueGetLength(t_queue *queue);
int queueAddRequest(t_pedido *request, t_queue *queue);
t_pedido queueGetRequest(t_queue *queue);

I'm having trouble importing the defined type into other files. I declared a main() function in queue.c and tested the functions from there, all working out fine. However, if I create some other file like test.c with the exact same code and try to compile it (gcc test.c) I'll get a strange error:
-->> test.c
#include "queue.h"
#include "funcoes_serv.h" //for type t_pedido

int main(){
//the exact same code that was on queue.c's main() function
t_pedido aux = queueGetRequest(&myQueue); //gcc says "incompatible types in assignment"
}

However, the code is exactly the same and I never did get the "incompatible types in assignment" error when testing from queue.c. And the types are correct, as it's possible to see. What am I doing wrong?

Thanks in advance :)
Avatar of bass20
bass20

ASKER

There's an #endif at the end of queue.h I just forgot to post it above
Avatar of Infinity08
Your .h file needs to contain the prototypes for the functions implemented in the .c file. ie. :

---- queue.h ----
#ifndef QUEUE_H
#define QUEUE_H

#include "comunic.h" //required for type t_pedido
#define QUEUE_LENGTH 16

typedef struct queue {
        t_pedido requests[QUEUE_LENGTH];
        unsigned int currentLength;
} t_queue;

void queueInit(t_queue *queue);
unsigned int queueGetLength(t_queue *queue);
int queueAddRequest(t_pedido *request, t_queue *queue);
t_pedido queueGetRequest(t_queue *queue);

#endif /* QUEUE_H */
---- ----
Avatar of bass20

ASKER

Adding the prototypes to the .h file will result in errors like:
/tmp/ccvt9JNq.o(.text+0xa2): In function `main': : undefined reference to `queueInit'
for each call that I make to a function on queue :S
The 3 files :

---- queue.h ----
#ifndef QUEUE_H
#define QUEUE_H

#include "comunic.h" //required for type t_pedido
#define QUEUE_LENGTH 16

typedef struct queue {
        t_pedido requests[QUEUE_LENGTH];
        unsigned int currentLength;
} t_queue;

void queueInit(t_queue *queue);
unsigned int queueGetLength(t_queue *queue);
int queueAddRequest(t_pedido *request, t_queue *queue);
t_pedido queueGetRequest(t_queue *queue);

#endif /* QUEUE_H */
---- ----

---- queue.c ----
#include "queue.h"

void queueInit(t_queue *queue) {
  /* implementation */
}

unsigned int queueGetLength(t_queue *queue) {
  /* implementation */
}

int queueAddRequest(t_pedido *request, t_queue *queue) {
  /* implementation */
}

t_pedido queueGetRequest(t_queue *queue) {
  /* implementation */
}
---- ----

---- test.c ----
#include "queue.h"

int main() {
  t_queue myQueue;
  t_pedido aux = queueGetRequest(&myQueue);
  return 0;
}
---- ----
make sure you included "queue.h" in test.c AND queue.c
why are you including two different header files for the same type ? (funcoes_serv.h and communic.h)
Avatar of bass20

ASKER

That's what I have currently; do I have to pass any special flag to gcc in order to compile them?
just :

  gcc queue.c test.c -o test
  ./test

or on Windows :

  gcc queue.c test.c -o test.exe
  test
Avatar of bass20

ASKER

That's what I'm doing :( If I copy paste the main function you just posted into the queue.c file and compile it it works perfectly, when I put it in another .c file, I get the compilation errors I mentioned :S
Did you use the three exact files i posted ? (just add the implementation for the 4 functions)

What does the communic.h file contain ?

Is there another object file ? (.c file)
Avatar of bass20

ASKER

The exact three files are giving me:
: undefined reference to `queueGetRequest'
when compiling test.c
Once again, if I move the contents of test.c into queue.c , compile it by "gcc queue.c -o queue" and run queue it all works fine
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of bass20

ASKER

Bullseye! Everything seems fine now, the problem was on the compilation command; I used the -c option the way you recommended and it worked perfectly :)

Thanks!