PDA

View Full Version : Using (old) C - have to declare EVERYTHING?




EddieDom
Jan 9, 2009, 12:28 PM
I'm porting over some old C code and wonder if there's a way to have to NOT HAVE TO include every function in a header as an external declaration?

E.g.

foo1.c

void doThis()
{
.... code ...
}
/* end of file */

foo2.c

void doThisFirst()
{
doThis();
....
}

/* end of file */

Generates an error when I compile foo2.c that "'doThis' was not declared in this scope".

I have hundreds of files like this and really don't want to have to declare every single function! Help!

Thanks.



zmttoxics
Jan 9, 2009, 12:48 PM
I'm porting over some old C code and wonder if there's a way to have to NOT HAVE TO include every function in a header as an external declaration?

E.g.

foo1.c

void doThis()
{
.... code ...
}
/* end of file */

foo2.c

void doThisFirst()
{
doThis();
....
}

/* end of file */

Generates an error when I compile foo2.c that "'doThis' was not declared in this scope".

I have hundreds of files like this and really don't want to have to declare every single function! Help!

Thanks.

That is what header files are for.
http://computerprogramming.suite101.com/article.cfm/c_header_files

EddieDom
Jan 10, 2009, 05:17 PM
I understand that this is one of the things that headers are for. As I said, this is OLD code and works fine in other IDEs (still). It used to be (through the 1980s) an assumed part of the dev process that the linker would deal with this.

I'm sure the updated C complires have made everyone more disciplined and I'm doing this on a go forward basis, but it did used to work and, I'm assuming there's some setting that allows this old fashioned style to work in the mac dev world (still works, for example, in Borland's IDEs).

EddieDom
Jan 10, 2009, 05:18 PM
I understand that this is one of the things that headers are for. As I said, this is OLD code and works fine in other IDEs (still). It used to be (through the 1980s) an assumed part of the dev process that the linker would deal with this.

I'm sure the updated C complires have made everyone more disciplined and I'm doing this on a go forward basis, but it did used to work and, I'm assuming there's some setting that allows this old fashioned style to work in the mac dev world (still works, for example, in Borland's IDEs).

Note that the reference is from 2006.. that's more than 20 years more recent than this coding style.

zmttoxics
Jan 10, 2009, 07:36 PM
I understand that this is one of the things that headers are for. As I said, this is OLD code and works fine in other IDEs (still). It used to be (through the 1980s) an assumed part of the dev process that the linker would deal with this.

I'm sure the updated C complires have made everyone more disciplined and I'm doing this on a go forward basis, but it did used to work and, I'm assuming there's some setting that allows this old fashioned style to work in the mac dev world (still works, for example, in Borland's IDEs).

Note that the reference is from 2006.. that's more than 20 years more recent than this coding style.

Headers weren't invented yesterday. The most common C standard is from the 80s, and hasn't changed. There are new standards - like c99, but they are not common. If you are having compile issues, perhaps providing some logs and code will help. Otherwise, header files are what you are looking for.

EddieDom
Jan 10, 2009, 09:12 PM
Get this, after fussing around with the compiler settings and reading the documentation about the -x compiler directive, turns out, that XCode does NOT recognize the file extension '.C'

Simply renaming the files with extension '.c' (i.e. lower case c) the files compiled file.

And, while header files were not invented yesterday, it was only moderately common in those days to declare functions in the header files (especially for functions with no return values and no parameters)... We can argue the point 'till the cows come home, but having spent the early part of my career fixing OPC, I would say it was a fairly common practice.

Thanks for your thoughts on the matter though, they did spur me on to find this (thankfully) remarkably simple solution!!!

idelovski
Jan 11, 2009, 02:13 PM
Oddly, but .C files are treated as C++ files and .c files as C files. You can change extension or change File Type settings for the file with Get Info to "sourcecode.c.c".

It is a feature of GCC, not Xcode.