I am trying to have a dictionary_object structure named dict_obj available in two separate .c files. This is while trying to write a pthread TCP server for a class. I have not worked much with C before and am having a difficult time getting this figured out. I’m not sure if I’m declaring this as an external structure correctly as netbeans is throwing off errors on clean and build saying invalid use of undefined type.
In db_functions.c I have:
//------------------------------------------------------------------------------
// Server Function & Variable Initialization
//------------------------------------------------------------------------------
struct dictionary_object dict_obj;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Server Function Codes:
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Database Initialization
//------------------------------------------------------------------------------
int db_initialization()
{
dict_obj.word_count = 0;
return 1;
In db_operations.c I have:
struct dictionary_object
{
char dictionary[DICTIONARY_SIZE][WORD_LENGTH];
int word_count;
pthread_mutex_t dict_mutex;
};
extern struct dictionary_object dict_obj;
Hopefully this gives you guys enough idea of what I’m trying to accomplish without having to overwhelm you with too much code. Thanks in advance! Let me know if you need to see more of the code.
You need to define the structure contents and layout once in an include file. Also define the “extern” definitions for all functions and statics for the code there. In any file wanting to use those structs or definitions, include the header file (something like db_struct.h).
db_struct.h:
db_struct.c:
Note this is all pretty old-style C code. Modern C++ etc. have other means of doing similar things.