--__--__--
Hi Dave,
Subject: [Alug] More C coding Woes
I have some code which when compiled gives this error
In file included from file1.c:4, from file2.c:6: llist.h:14: redefinition of `Node_ptr' llist.h:14: `Node_ptr' previously declared here llist.h:17: redefinition of `struct Node`
First thing Could be including the same file twice within the same run (pre-compiler directives should prevent this) At the top of the file llist.h is there a directive similar to : #ifndef LLIST_H #define LLIST_H
if there isn't add one. At the bottom of the file you will also need a directive : #endif to close it off.
This also works nicely around the include directives themselves, can spped things up on large projects since the preprocessor doesnt need to open the file and parse it before 'realising' that the file has already been installed. #ifndef FILE_H #include <file.h> #endif will protect it at the point of inclusion. HTH
Can anyone explain what I am doing wrong. I know its something todo with #includes and where they are in which files etc...
Thanks
D
--- Earl Brannigan earl.brannigan@lindenhouse.co.uk wrote:
--__--__--
Hi Dave,
Subject: [Alug] More C coding Woes
I have some code which when compiled gives this error
In file included from file1.c:4, from file2.c:6: llist.h:14: redefinition of `Node_ptr' llist.h:14: `Node_ptr' previously declared here llist.h:17: redefinition of `struct Node`
First thing Could be including the same file twice within the same run (pre-compiler directives should prevent this) At the top of the file llist.h is there a directive similar to : #ifndef LLIST_H #define LLIST_H
if there isn't add one. At the bottom of the file you will also need a directive : #endif to close it off.
Tried this, it didn't work, it transfered the errors to the linker not the compiler.
This also works nicely around the include directives themselves, can spped things up on large projects since the preprocessor doesnt need to open the file and parse it before 'realising' that the file has already been installed. #ifndef FILE_H #include <file.h> #endif will protect it at the point of inclusion.
R, where should the #ifndef bit go then? in which files?
Thanks
D
HTH
Can anyone explain what I am doing wrong. I know its something todo with #includes and where they are in which files etc...
Thanks
D
alug, the Anglian Linux User Group list Send list replies to alug@stu.uea.ac.uk http://www.anglian.lug.org.uk/ http://rabbit.stu.uea.ac.uk/cgi-bin/listinfo/alug See the website for instructions on digest or unsub!
===== -------------------- "We all know Linux is great... it does infinite loops in 5 seconds." Linus Torvalds
__________________________________________________ Do You Yahoo!? Get personalized email addresses from Yahoo! Mail http://personal.mail.yahoo.com/
David Freeman wrote:
--- Earl Brannigan earl.brannigan@lindenhouse.co.uk wrote:
In file included from file1.c:4, from file2.c:6: llist.h:14: redefinition of `Node_ptr' llist.h:14: `Node_ptr' previously declared here llist.h:17: redefinition of `struct Node`
First thing #ifndef LLIST_H #define LLIST_H #endif to close it off.
Tried this, it didn't work, it transfered the errors to the linker not the compiler.
This also works nicely around the include directives themselves, can spped things up on large projects since the preprocessor doesnt need to open the file and parse it before 'realising' that the file has already been installed. #ifndef FILE_H #include <file.h> #endif will protect it at the point of inclusion.
R, where should the #ifndef bit go then? in which files?
"We all know Linux is great... it does infinite loops in 5 seconds." Linus Torvalds
Does the kernel kill a busywait process then ?!?
This is a superb quote but I can't nick it 'til I can justify it to curious people...
Anyway, what you need is something like...
llist.h: #ifndef LLIST_H #define LLIST_H
typedef struct LList_Node LList_Node; LList_Node { LList_Node* next; int data; /* replace with void* if you want something useful */ };
#endif
You should be able to #include that in main.c and it will gurgle happily. I think in modern C compilers you can combine the typedef and {} thus: typedef struct LList_Node { /* stuff */ }
but I can't remember. You could, of course, start using C++:
#include <queue>
queue<int> my_q;
int main() { my_q.push(6); printf("%d\n", my_q.front()); my_q.pop(); return 0; }
However, C++ is the Dark Side. I have never yet been able to do something even moderately complex without needing Stroustrup. Ever tried static data, anyone? Yuck.
Alexis -- Mozilla ate my mutt. Sniff.