Advertisement

C code Organization

Started by March 06, 2003 04:16 PM
1 comment, last by Jess 21 years, 8 months ago
I have lot of troubles with organizate the code in C (I''m not talking about C++). I am making a OpenGl program that works in only .c file, or separate in a winmain .c file and 4 .h headers, my GLDraw function that it is only in one header file, but if in this GLDraw function I want to use a variable that is used too in winmain.c file I get only errors or no results in screen. I have tested extern and static, but it doesn''t works on me, or I don''t know to use it right. Anyone can help me or tell me some links about organization C code in modules? (remember not C++ like the article in this web)
http://cplus.about.com/library/weekly/aa060402d.htm
http://www.subduck.com/vb/showthread.php?s=&threadid=89

First two links from a google search for "multiple files c".
Haven''t looked at them exactly, but they seem usable.

The basics in short:

- Don''t put any code in header files, only forward declarations and other declarations (structs, typedefs...). You would get duplicate definitions otherwise.

- If you want to use a variable from another file:

  // file1.cint a; // variable is declared here// file2.cextern int a; // This variable exists in some other file  


"George W. Bush Geography Simplification Initiative"

More info on George W. Bush
My Homepage (C++ SDL OpenGL Game Programming)

I am a signature virus. Please add me to your signature so that I may multiply.
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
Advertisement
In a nutshell, group functions that operate on similar items together in the same source file and add source files to the project (don''t include them like headers). Typically, function prototypes, structure typedefs and preprocessor defines associated with a particular source file are placed into the corresponding header for that file. The primary factor to decide whether a given item should be put into the header or left in the source is whether or not that item should be reachable by other source files. For example, lets say your project has two source files, winmain.c and gldraw.c. There are functions in gldraw.c that you want to call from winmain.c, so you put the prototypes for those functions into gldraw.h and then add #include "gldraw.h" to winmain.c. If there are other functions in gldraw.c that you don''t want to call from winmain.c - and that you don''t plan on calling from any file other than gldraw.c - don''t put those prototypes into gldraw.h. These functions can also be declared static. That limits their use to the file where they are declared. This is different than declaring a variable inside of a function to be static. The extern keyword tells the compiler to hold open a slot for the item. It''s sort of like saying, whatever it is, this thing here is defined externally - ie in some other file in this project.

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement