#### Archived

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

# Dev-C++ includes

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

## Recommended Posts

I am using Dev-C++. I made a project with three files: main.cpp, definitions.h, and graphics.cpp. I #included the other two in main. When I tried to compile, it got tons of errors. I removed the other two from the project, but still #included them in main, and it worked. I thind the compiler tried to compile each file in the project separately. How can I make it only compile the one ''main'' file in a project? I don''t want to have to dig through the project folder (eventually, when it is much bigger) to look at some snippet of code.

##### Share on other sites
NEVER include a cpp file... They are automatically thrown in by the compiler...

##### Share on other sites
No, I believe what happened was that not only did you include them, but the compiler included them as well because they were part of the project. This would lead to a lot of duplicate definition errors. I''m not an expert, so I may be wrong, but I think this is what happened.

(Stolen from Programmer One)
UNIX is an operating system, OS/2 is half an operating system, Windows is a shell, and DOS is a boot partition virus

##### Share on other sites
That is exactly the opposite of what you want to do. You''re right, Dev-C++ builds each cpp file individually - that''s the whole point of C''s compiler model.

Each .cpp is compiled (by gcc in this case) into a .o (object) file, which the linker (ld in this case) then takes and links with all of the other .o files generated from all the other .cpp files. Mix in any included libraries, shake, and viola - program compiled.

Putting everything in one file is EvilTM. Just don''t #include the graphics.cpp file in main.cpp, and let Dev-C++ do what it''s supposed to do.

##### Share on other sites
If I just let the compiler do its thing, it works almost perfectly. The huge amounts of errors are gone. The only problem is that main doesn''t recognise the class defined in character. How can I tell the compiler that main depends on the external class ''Character''? I tried extern, but it might have been bad syntaxt.

##### Share on other sites
quote:
Original post by nagromo
The only problem is that main doesn''t recognise the class defined in character. How can I tell the compiler that main depends on the external class ''Character''?

In which file is the Character class defined? If it''s in a header (.h) file, go ahead and include it and it should work. If it''s in a .cpp file, then you need to strip the class''s declaration out of that file (so it only contains the class implementation), put it in a header file, and include the header file where necessary.

Don''t forget the #ifndef/#define...#endif in the header file, or you''re gonna have more problems.

yckx

##### Share on other sites
I always use the #ifnef stuff in .h files. Here is how I have it now:

Character.h: class definition
Character.cpp: class member functions
main.cpp: main file

Where should I #include Character.h? If I include it in main, the Character.cpp file doesn''t recognize the member functions

Character::Character(u16 x,...) { [syntax error before ''::'' token]

but if I include it in Character, main still can''t use the class.

I know my code is OK, because if I copy and paste it all into main it works, but as the project grows that would be a horror.

##### Share on other sites
//main.h
#include everything that all files need.
#define all datatypes
#define all your functions from every file
***
//main.cpp
#include "main.h"
#include <something ONLY main needs>
***
character.cpp
#include "main.h"
#include <something ONLY character.cpp needs>

##### Share on other sites
I did that and it finally works!

Wouldn't that lead to repetitive includes, though? As I get more cpp files, I don't want to include my entire API and definitions in each file.

Another thing that might work is to say that main depends on definitions in character. Is there a way to tell it what file depends on other files without using makefiles?

[edited by - nagromo on November 27, 2003 12:21:55 PM]

##### Share on other sites
That''s why modulirization is good. If your api is split in several .h files, each of which makes sense on its own, then you don''t have to include the headers for the ENTIRE API in each other CPP file; only the headers for the modules you actually need functions from.

However, "unnecessary includes" don''t really cost anything; they''re already in the file cache; parsing them should take a few milliseconds (literally). If you program for Windows, you probably #include <windows.h> which includes like 80% of the Win32 API (not very modularized :-) and that doesn''t hurt too much. For large projects, there''s also pre-compiled headers on many compilers.

Anyway, the model that''s defined by the compiler and linker is that each CPP file is compiled as its own thing, generating a O or OBJ file. The compiler or development environment do not keep any state between each successive compile. Thus, if you want to use a function in two separate CPP files, you need to include the prototype of the function (from a header) in each of the CPP files. The compiler is a separate program that''s started for each CPP file by the "make" utility or development environment, and which terminates when it''s done compiling a single CPP file.

Once you have all CPP files turned into O/OBJ files, your make or build environment will probably call the linker to compose all those O/OBJ files, together with libraries that you specify, or which are default for your environment, into an executable. Here is where you get "missing symbol" or "duplicate definition" errors. I find that it''s quite important to understand exactly how symbol and references work, are generated, and are resolved, because otherwise you will be forever confused about the exact roles of your different tools, which leads to pain.

##### Share on other sites
There a post recently about C''s #include system sucking. This is why.

For each class, have one .cpp file and one .h (or .hpp) file. Put the class definition in the .h and the implementaion in the .cpp. Then, only inlcude the .h files where thay are needed. Don''t put everything in one header file.

There a some things I like about Java, and some things I hate, but the modules are what I like most.

//3DObject definitions#ifndef OBJECT3D_H#define OBJECT3D_H//--------------------------------------#include CObject.h#include CSprite.h#include C3DSModel.h#include CMD2Model.h#include CMD3Model.h//etc...//--------------------------------------#endif