Page 1 of 2
Question about C
Posted: Fri Dec 08, 2006 10:23 am
by braindamage
googled for a while but haven't found anything
don't know what to search for...
so i hope you can help my...
here is an example of some of my files
----------------------------------
video.c //includes video.h
video.h //includes bla.h
bla.c //includes bla.c
bla.h
kernel.c /inludes video.h
----------------------------------
my kernel.c includes video.h
but i don't want kernel.c to see
methods defined in bla.h.
how can i archive this??
i'm not sure if c can handle this or
if it is a linking issue
would be happy to get some advice
thx
braindamage
Posted: Fri Dec 08, 2006 11:23 am
by Walling
Why not just do:
Code: Select all
"video.c" - includes "video.h" and "bla.h" (because it uses some of its functions?)
"video.h"
"bla.c" - includes "bla.h"
"bla.h"
"kernel.c" - includes "video.h", but not "bla.h"
Would that work?
Posted: Sat Dec 09, 2006 6:11 pm
by Solar
@ braindamage:
The way you pictured the includes, no, you cannot do this. Walling pointed out an alternative.
Note that "being visible" is somewhat fuzzy when used in relation with includes. If you #include a header, that usually means you include function declarations, some datatype definitions (structs and typedefs), and a couple of #defines.
But if, for example, you use the function strcpy() without including <string.h>, your program would still compile - depending on your compiler settings, a warning might be generated. C does not require function declarations.
If you have a function foo() in bla.c which you do not want to be visible outside of bla.c at all, use the "static" keyword.
And try to organize your headers in a way that they do not include other headers, unless strictly necessary. The problem you have right now is only one inconvenience from a wide range of headaches that can arise from careless includings...
Posted: Sat Dec 09, 2006 8:05 pm
by earlz
[quote]C does not require function declarations. [/quote[
I beg to differ!
they aren't required for MOST function related stuff but to get the pointer to the function it must be declared!
also it's good to include them in ide's because most of them will parse it for you and give you nifty code completion
Posted: Sun Dec 10, 2006 12:41 pm
by Solar
Function pointers... you might be right, I never tried to do things without declarations. Not only do IDE's give you nifty functionality, but any compiler stands a much better chance of giving you a compile-time warning if your code is bad.
Posted: Sun Dec 10, 2006 1:42 pm
by earlz
lol, I hate warnings..since my project has reached the "over 100 warnings" point, I just ignore them..
and I think my code looks better with the warnings rather than have (unsigned int) and (void *) everywhere...
for instance...
Code: Select all
unsigned char *ptr;void *core;
(unsigned int)ptr=(unsigned int)ptr-(unsigned int)core;
I can't get rid of the warning made by that code.. like that it gives "use of cast expressions as lvalues depricated"
and if I remove all the (unsigned int) stuff then it says something like "makes a pointer from an integer" or something about arithmetic...
Posted: Sun Dec 10, 2006 4:00 pm
by earlz
The casts are a bit ugly, but a least it'll get rid of the warning. In any case, either one will break horribly on platforms where sizeof(void *) doesn't equal sizeof(int).
and their is why I hate warnings...most of the tim it warns about something that would make your code next to unreadable and also a lot of times it prevents it from being portable
Posted: Sun Dec 10, 2006 4:09 pm
by earlz
inhibit all warnings
but according to C standard I think your suppose to use a temporary variable to do it
Posted: Sun Dec 10, 2006 4:10 pm
by Combuster
There are always methods to make code more readable whilst not getting warnings.
Learn to bow for -Werror (or the msvc variant) and at some point you will stop noticing its being issued.
Or is it just me that compile warnings are a sign of hackish code?
Posted: Sun Dec 10, 2006 4:39 pm
by Brynet-Inc
Combuster wrote:There are always methods to make code more readable whilst not getting warnings.
Learn to bow for -Werror (or the msvc variant) and at some point you will stop noticing its being issued.
Or is it just me that compile warnings are a sign of hackish code?
Going to have to agree, I've seen your code hckr83..
Warnings are avoidable..
Ignoring them is not a good idea...
-Werror is a nice flag
You could pick an ISO standard:
-ansi
-std=c89 (or -std=iso9899:1990)
-std=c99 (or -std=iso9899:1999)
Or with GNU extensions:
-std=gnu89 (for C89 with GNU extensions)
-std=gnu99 (for C99 with GNU extensions)
And then you could turn on the -pedantic (or -pedantic-errors) flag..
There are various helpful STDC predefined macro's listed
here.
GCC supported standards
http://gcc.gnu.org/onlinedocs/gcc/Standards.html
Various additional links..
http://www.open-std.org/jtc1/sc22/wg14/www/standards
http://www.open-std.org/jtc1/sc22/wg14/ ... /n1124.pdf
http://anubis.dkuug.dk/JTC1/SC22/WG14/w ... 869.txt.gz
Posted: Sun Dec 10, 2006 4:59 pm
by earlz
I think I'll keep my code at least halfway readable rather than adding globs of (char *) and (unsigned int) stuff..
Posted: Mon Dec 11, 2006 2:45 am
by Solar
Combuster wrote:
Or is it just me that compile warnings are a sign of hackish code?
No, you aren't.
Hmmm.... how to put this politely?
There is
no reason why code shouldn't be
both readable and warnings-free. Ignoring them for reasons of "readability" (or worse yet, "portability") just means you won't catch the one warning that actually tells you about a problem that could cost you hours of debugging.
Ignoring warnings is barely acceptable for ad-hoc code. For a project, it's a very, very poor design decision. Someone else building your code won't know which warnings are OK to ignore and which are genuine problems...
Code: Select all
unsigned char *ptr;void *core;
(unsigned int)ptr=(unsigned int)ptr-(unsigned int)core;
That isn't "readable", that's completely obfuscating what you are intending to do. Since I don't know what you want to do, I can't tell you how to write it warnings-free.
And -Wall -Werror isn't even half of it, as -Wall is a misnomer. PDCLib compiles warnings-free using:
Code: Select all
CFLAGS := -Wall -pedantic -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -Wno-long-long -Wconversion -Wstrict-prototypes
That isn't meant as boasting, it's meant to show you that it is possible to write complex, portable code without ignoring warnings.
Posted: Mon Dec 11, 2006 3:18 am
by Solar
Brynet-Inc wrote:And then you could turn on the -pedantic (or -pedantic-errors) flag...
Note that "-pedantic" does not refer to "pedantic error-checking" but rather "pedantic adherence to what the standard says". At some points the standard requires a diagnostic where "GNU style" doesn't, and vice versa.