• C

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 :)
LVL 1
bass20Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bass20Author Commented:
There's an #endif at the end of queue.h I just forgot to post it above
Infinity08Commented:
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 */
---- ----
bass20Author Commented:
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Infinity08Commented:
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;
}
---- ----
Infinity08Commented:
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)
bass20Author Commented:
That's what I have currently; do I have to pass any special flag to gcc in order to compile them?
Infinity08Commented:
just :

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

or on Windows :

  gcc queue.c test.c -o test.exe
  test
bass20Author Commented:
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
Infinity08Commented:
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)
bass20Author Commented:
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
Infinity08Commented:
>> : undefined reference to `queueGetRequest'
Either there's something missing in one of the three files, or you're using the wrong compilation command.

For the first, double-check the contents of the files (copy-paste if need be). Also see what's in the communic.h file ... is there another compilation unit ?
You could also try to define the queue type like this :

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

and NOT include the communic.h file.

For the second, try recompiling this way, and look for compilation errors :

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bass20Author Commented:
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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.