[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

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 :)
0
bass20
Asked:
bass20
  • 6
  • 6
1 Solution
 
bass20Author Commented:
There's an #endif at the end of queue.h I just forgot to post it above
0
 
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 */
---- ----
0
 
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

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

or on Windows :

  gcc queue.c test.c -o test.exe
  test
0
 
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
0
 
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)
0
 
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
0
 
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
0
 
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!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now