Public Group

#### Archived

This topic is now archived and is closed to further replies.

# C code Organization

This topic is 5588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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)

##### Share on other sites

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"

My Homepage (C++ SDL OpenGL Game Programming)

I am a signature virus. Please add me to your signature so that I may multiply.

##### Share on other sites
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.

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
18

• 11
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631420
• Total Posts
2999990
×