c++ file structure

Hi,

As I start to write more and more complex stuff, I am beginning to wonder a few things.

- If I want to put a library full of frequently used functions and just include that in my main program, what should its extension be? .cpp?

- Whats the difference between a .cpp and .h ?

- How come some of them have to be in the g++ command (linked?) and others dont?

Ive found some good stuff with google, but not much that answers basic questions like these.
In headers ( .h ) you have the declarations (and the implementations for inline functions or templates).
In source files (.cpp) you have the definitions.
When you use #include "some header.h" that line is replaced by the contents of the file "some header.h"
When compiling a program you are compiling (and linking) source files, headers don't get compiled

If your frequently used functions are inlined you can just use your own header, if they aren't you should create both a header and a source file then you can decide whether to build a static or dynamic library or compile and link your cpp file each time
Last edited on
Thanks guys.

Originally I was working in komodo and compiling on the command line with g++ ... I had a shell script that had the g++ command with all the "-I, -L" etc in it.

Then as the project grew, I decided to pull it all into codeblocks, which i am using for the first time.

This obviously made me have to think about organizing the code, thus my original questoin.

But codeblocks .. something about how its compiling, is generating errors that the command line didnt.

Stuff like this, in my included .cpp file:

1
2
inline std::string TrimStr(const std::string& Src, const std::string& c = " \r\n"){
//includes.cpp|1|error: ‘string’ in namespace ‘std’ does not name a type 


and

1
2
3
4

vector<string> explode( const string &delimiter, const string &str){

//includes.cpp|10|error: expected constructor, destructor, or type conversion before ‘<’ token| 


So its kinda driving me nuts. Also it found a few "warnings" that the command line didnt.

This has been a great (and frustrating) learning experience however!
Are you #including all needed headers?
I believe so .. I have

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "md5wrapper.h"
#include <iostream>
#include <string>  // this should relate to my errors, right? 
#include <unistd.h>
#include <ctime>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <cerrno>
#include <mysql++.h>
#include <fstream>
#include <map>
#include "includes.cpp" // this is my file 
#include "includes.h" //just prototypes in here 


It might be something about how codeblocks is doing things. Im looking thru all the menus, but not finding anything at the moment.
You are missing <vector>
You shouldn't #include cpp files (they should be compile and linked)

codeblocks compiles all cpp files unless you explicitly tell it not to do that:
right click on the file you don't want to compile > Properties... > Build > select the options you want
Ahh yeah I saw the <vector> as soon as I posted that.

So, one question about what you said:

So right now, by default, codeblocks is compiling and linking the file. This is why it isnt working?

If I remove the includes.cpp .. how will it know to compile and link it, or will it just do it because its part of the project?

You indicated that best practice would be to compile and link, rather than include, so I'd rather do it right.

So .. if I remove the include references to includes.cpp -- would I then have to make includes.cpp have its own main() , and its own #include for any functionality it needs?

Thanks again!


You can have only one call of main() as it is the entry point for your program.
You should #include all headers needed by includes.cpp.
To use symbols defined in includes.cpp from other files you should put the declarations on a header file ( includes.h ? ) and #include it in all cpp files using those symbols
As I said before, for default codeblocks compiles cpp files which are in the project
Topic archived. No new replies allowed.