Question about C

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
braindamage
Posts: 1
Joined: Fri Dec 08, 2006 10:14 am

Question about C

Post 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
User avatar
Walling
Member
Member
Posts: 158
Joined: Mon Dec 04, 2006 6:06 am
Location: Berlin, Germany

Post 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?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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...
Every good solution is obvious once you've found it.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post 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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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.
Every good solution is obvious once you've found it.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post 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...
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post 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
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

inhibit all warnings :D
but according to C standard I think your suppose to use a temporary variable to do it
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post 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?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post 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 8)

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
Last edited by Brynet-Inc on Sun Dec 10, 2006 5:00 pm, edited 1 time in total.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

I think I'll keep my code at least halfway readable rather than adding globs of (char *) and (unsigned int) stuff..
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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.
Last edited by Solar on Mon Dec 11, 2006 3:21 am, edited 1 time in total.
Every good solution is obvious once you've found it.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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.
Every good solution is obvious once you've found it.
Post Reply