• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Fwd: external declares
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Fwd: external declares


  • Subject: Fwd: external declares
  • From: Steve Checkoway <email@hidden>
  • Date: Sat, 11 Feb 2006 00:41:39 -0800

Oops, I clicked "Reply."

Begin forwarded message:

From: Steve Checkoway <email@hidden>
Date: February 10, 2006 10:17:32 PM PST
To: Robert Dell <email@hidden>
Subject: Re: external declares


On Feb 10, 2006, at 5:25 PM, Robert Dell wrote:

they seem to think this is off topic for the cocoa list so i'll ask here.

i broke up my 40,000 line program into smaller includes. one of which is the reqs calculator section and all routines needed to do that.

I don't think that including the files is the way to go. I've seen a few instances where .c files were included (PCRE springs to mind), but in general this is not the right approach. The best way is to break the file up into related functions and put those groups of related functions into separate files. There's no advantage to just including .c files (PCRE does it because it has a static table that's rather large and would clutter up the main .c file, iirc).


i noticed i needed to declare my variables as external references such as:
(within reqs.c)
extern long currentCircle;
extern long nextCircle;
extern long calcCircle;


but when i got to this part, i noticed i had forgotten to declare this as external but it still worked...
(from within the main program file, not the included reqs.c file)

When you include the file, it is no longer external. Declaring variables as having external linkage just informs the compiler that there is a global variable with that particular name and type _somewhere_. In this case, it's in the same file (since you're just including the other file).


struct expskill
{
char skillname[80];
long skillvalue;
long skillreqs;
long skillNeeded;
long skillexclude;
} myarmor[8], myweapon[25], mymagic[5], mysurvival[15], mylore [13], myinstruments[5], swap;


later on, i have the following in the main program file
#include "reqs.c"

so far, so good...

Well, not really good. You should have a header file that contains the function prototypes that are defined in reqs.c and include that header file.



my next question is dumbfounding to me...
why did the compiler scream at me when i didn't declare "nextCircle" as an external variable or at all because of "undeclared identifier" but it allowed "myarmor[1].skillvalue"
where did it get it's information from to know myarmor is a structure and it's dimensions but NOT know nextCircle is a simple longint value?


mind you, the compiler errored out before i added all the extern long declarations.

I'm not really following you here.

Some things to keep in mind, #include "file" has the effect of copying "file" in its entirety (after preprocessing) into the output file. Let me demonstrate:

steve$ cat foo.c
int foo()
{
        return 5;
}
steve$ cat main.c
#include "foo.c"

int main()
{
        return foo() != 5;
}
steve$ gcc -E main.c
# 1 "main.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "main.c"
# 1 "foo.c" 1
int foo()
{
return 5;
}
# 2 "main.c" 2

int main()
{
return foo() != 5;
}

As you can see, after preprocessing, #include "foo.c" has been replaced by the contents of foo.c

What you want to do is to have a file foo.h which is included in main.c

steve $ cat foo.h
int foo();

Then you would compile both .c files to produce object files and link those together.

This is what people meant when they suggested that you break up your one massive file.

Another thing is if you cannot or will not break it up that way, then you don't really gain anything from including .c files in other .c files. In this case, you might as well leave it as one 40,000 line file.

- Steve

Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Re: variable weirdness
  • Next by Date: Re: variable weirdness
  • Previous by thread: Re: external declares
  • Next by thread: Defining App Run-time Environment Variables
  • Index(es):
    • Date
    • Thread