Multiple files C project.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 22
Reputation: Layla_2401 is an unknown quantity at this point 
Solved Threads: 0
Layla_2401 Layla_2401 is offline Offline
Newbie Poster

Multiple files C project.

 
0
  #1
Apr 29th, 2005
Hi all,

I have a C project consisting of five files as follows:

main.c
file1.c
file1.h
file2.c
file2.h

I have defined an array in file file1.h as follows:

  1. static struct prnc keys[MAXP];
the array was populated in file file1.c

Now I need to use the contents of this array again in file file2.c , I don't get a compilation error when i try to print the cotents of the array in file2.c, but at run time I get nothing as if the code is never encountered. So I tried the following two approaches:

1- I changed the definition of the array in file1.h to:

  1. extern struct prnc keys[MAXP];
This time I got a compilation error generated by references to the array in file file1.c, the error message stated that the array is unknown. Why?
so I tried this:

2- I kept the definition in file1.h as:

  1. static struct prnc keys[MAXP];
and added the following line in file2.c:

  1. extern struct prnc keys[MAXP];
the project compiles but at run time the array seems to be empty.

My question is:

The array is populated in file1.c, and I want to reference its contents in
  1. file2.c
, how can I do that? cause I really don't think that answer is to repopulate it in file2.c! or is it?!


Thanks in advance for your help.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Multiple files C project.

 
0
  #2
Apr 29th, 2005
Lose the static qualifier. extern should be placed on the array in the header file to ensure that it's a declaration. Then include file1.h in both file1.c and file2.c. Make sure that the code to fill the array is called first if you define it in file1.c.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 22
Reputation: Layla_2401 is an unknown quantity at this point 
Solved Threads: 0
Layla_2401 Layla_2401 is offline Offline
Newbie Poster

Re: Multiple files C project.

 
0
  #3
Apr 29th, 2005
That's exactly what I did the first time and which resulted in the compilation error:
"undefined reference to 'keys' "
where keys is the name of the array.
I'm using bloodshed Dev C++ compiler if it makes any difference.
Suggestions?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 22
Reputation: Layla_2401 is an unknown quantity at this point 
Solved Threads: 0
Layla_2401 Layla_2401 is offline Offline
Newbie Poster

Re: Multiple files C project.

 
0
  #4
Apr 29th, 2005
Ok check this out,

I defined the array in file1.h as:

  1. struct prnc keys[MAXP];

then, I included the following in file2.c:

  1. extern struct prnc keys[MAXP];

and it worked!
Please let me know if you think this is a poor scheme or that I should go about this in another way.
But either way I really appreciate your help, Thank you : )
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Multiple files C project.

 
0
  #5
Apr 29th, 2005
>Please let me know if you think this is a poor scheme
I think it's a poor scheme, but since it works for you and you can't seem to figure out how to set up your file dependencies properly, go ahead and use it.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 22
Reputation: Layla_2401 is an unknown quantity at this point 
Solved Threads: 0
Layla_2401 Layla_2401 is offline Offline
Newbie Poster

Re: Multiple files C project.

 
0
  #6
Apr 30th, 2005
But I want to know the proper way if you can kindly tell me,

file1.c has the following header files:

  1. #include "file1.h"

file2.c has the following header files:

  1. #include "file1.h"
  2. #include "file2.h"

But applying the scheme you're suggesting -which is approach #1 in my first post- does not work.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Multiple files C project.

 
0
  #7
Apr 30th, 2005
>But I want to know the proper way if you can kindly tell me
I did kindly tell you. Use extern declarations in your header files, provide a single source file for definitions, and the linker will do the rest. You only need a declaration for the code to compile.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3029 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC