Quick Question

Started by
4 comments, last by Miraj 24 years, 3 months ago
Hi Gang, Even though I have a somewhat good knowledge now of C/C++, until recently all of the programs I have written were contained within one source file. This was fine considering my programs were fairly small and simple in nature. Now that they are becoming quite large and a bit more complicated they are at times quite difficult to manage. So in an effort to keep my code better organized and managable I''d like to spread the code out across multiple files. My question is this... Does the compiler take care of this on its own or do I need to make a few modifications in the code or elsewhere? For example, say I have a file with a bunch of functions. If I call a few of them in another file will it recognize it? I would just go ahead and try it out myself but I just formatted my computer and besides not having installed my compiler yet I thought this would be a good way to stop staring at all of these progress bars while I install my software again, heh Thanks in advance and have a great day. -Miraj
-Miraj
Advertisement
Well, it''s about half and half. You are going to have to do a couple things with your code to get everything to work right. Basically, this involves working with header files and declaring your functions. Now as far as what the compiler does, today''s compilers will automatically generate all of your files and link them correctly, provided that you wrote the code correctly.

I know this probably isn''t the best explination. This is just one of those things that you are going to have to do to get good at.
What you have to do is this:

1. Create a new .c/.cpp file and enter some code in it
2. create a .h file with the same name as the above C/C++ file which should include the declarations of constants/functions/globals used for this source
3. Include the header file in your main file (after all the standard/DirectX/OpenGL files) and the compiler will compile and link them together if the code or declaration aint broken

The dictionary is the only place where success comes before work.
Death is lifes way saying your fired.
Think of your header (.h) files as advertising the functions that are in their source (.c/.cpp) files. You can write a function in a.cpp, but unless you declare the prototype of that function in a.h, no other program is going to know about it. Also, if you want another source file (say b.cpp) to know about your function in a.cpp, you have to add #include "a.h" to the top of b.cpp.

Good luck!
Thanks guys for the insight and advice. I had a sneaking suspicion that headers were involved but I wasn''t sure. Now I just need to get it working. Thanks again and may the force be with you.



-Miraj
-Miraj
There''s a thread already in the forum titled "Source code separation" that deals with this topic in a little more detail. As of right now it''s last post was on 1/16/00. You might want to check it out if you have any problems seperating your files.

This topic is closed to new replies.

Advertisement