Sign in to follow this  
codingPractices

How to structure a codebase

Recommended Posts

I currently have a folder "C++" and within it a bunch of projects. It looks a little like this (obviously I haven't named them Project1 etc.):

    C++

        Project1

        Project2

        Project3

Now this used to work fine up until I started having the need to use a class that I made from another project. I thought of copy-pasting the class but this wouldn't work as if I updated the original class, the copied ones wouldn't be updated. Also, I'd have to guess in which project I made the class in. Then I thought of calling it from the other project itself but then if I were to package the project it wouldn't work as it wouldn't include the classes from external folders (at per my knowledge).

The only other thing I can think of is within my "C++" folder I have a folder named something like "Codebase", where I store all the classes and the like, and another folder called "Projects" or something and keep "Project1", "Project2" etc. in there and call the classes from the codebase (thus solving the problem where I can't find where a specific class I made is). However, again, I don't know packaging the final product would work. How do you, or professionals, people on GitHub etc. do it?

Share this post


Link to post
Share on other sites
This is a problem that pretty much every no-suck revision control system can solve for you.

Import the externals you need from the repo, and there you go. Original is modified and committed, the other projects using it are updated as well. You can (and probably should) make the shared code explicitly a "library", but this is not strictly necessary. It's much nicer, though.

But... you are not using a revision control system? Shame on you. Shame, shame, shame. (No, really... you absolutely want to use one, not just for this purpose.)

Share this post


Link to post
Share on other sites

1. Classes aren't 'called' or 'packaged' in C++. That's not how the language works. The compiler doesn't care where the files live, providing it can find them.

2. This question doesn't make sense. If you're asking "how do I designate a certain part of my code as a library" then that depends what sort of library you're making. At this stage, I don't recommend you attempt to make anything explicitly into a library, and just keep your shared code in a shared directory.

 

I'm not sure this is an issue where a revision control system helps, but it would complicate matters a lot.

Share this post


Link to post
Share on other sites

main.cpp 

#include "/home/neel/MEGA/Code/Test/OtherProject/MyClass.h"
#include <iostream>
using namespace std;

int main(){
    cout << "come on\n";
    MyClass myClassObject;
    myClassObject.myFunction();
}

It includes the file but when I instantiate it it says "undefined reference to MyClass::myFunction()"

 

Here's the header and source file included in a different folder:

 

MyClass.h

#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass{
    public:
        void myFunction();
};

#endif

MyClass.cpp

#include "MyClass.h"
#include <iostream>
using namespace std;

void MyClass::myFunction(){
    cout << "hello bob\n";
}
Edited by codingPractices

Share this post


Link to post
Share on other sites

The #include will locate the header file, but the implementation of the class is presumably in a .cpp file, and that doesn't appear to be referenced in your project. Without that, it can't access the actual code for that class and can't build the final executable.

 

If you're using Visual C++, ensure both those files are in your project, the header and the source file. If you're using a different compiler, how to do this will differ.

Share this post


Link to post
Share on other sites

To clarify, I use Gedit and g++ with Terminal (basically I don't use an IDE)

 

You need to tell the compiler where to look for include directories. For g++, it looks like you want the -I compiler option.

 

So do I keep all the code the same and then just change the command from Terminal or?

 

The #include will locate the header file, but the implementation of the class is presumably in a .cpp file, and that doesn't appear to be referenced in your project. Without that, it can't access the actual code for that class and can't build the final executable.

 

If you're using Visual C++, ensure both those files are in your project, the header and the source file. If you're using a different compiler, how to do this will differ.

 

 

Using g++, how would I do this?

Share this post


Link to post
Share on other sites

Depends on what IDE you are using. With g++ you pass both the directories where libraries can be found (-L (large L)), the directories where header files can be found (-I (large 'i')), and what libraries you want to link against (-l (small 'L' - no, I'm not kidding)).

 

Most IDEs handle that for you, though - or makefiles.

 

If you are going to use libraries, make sure they are static not dynamic, and you'll save yourself ten weeks of headaches.

 

I organize my code like this:

Projects/                                 //(doesn't need to be language specific!)
Projects/StrangerFlames/
Projects/StrangerFlames/Common/           //My "Common" library (a static library). Really needs to be broken up into sub-libraries when I have time.
Projects/StrangerFlames/CommonTests/      //Test cases for Common. Ostensibly.
Projects/StrangerFlames/Engine/           //The game logic itself.
Projects/StrangerFlames/Platforms/          
Projects/StrangerFlames/Platforms/Desktop/                  //Builds the .exe for Win/Mac/Linux PCs.
Projects/StrangerFlames/Platforms/DesktopWithEditor/        //Builds the .exe for Win/Mac/Linux PCs, but includes the built-in Editor.

Ofcourse, Projects/StrangerFlames/ is in a repo (I use Mercurial (aka 'hg') with the free BitBucket service, but git (and the free GitHub service) is more popular).

 

The common library is under the StrangerFlames repo, but at present I don't mind. If I work on another project, I don't want to be afraid of making changes that'll break StrangerFlames, so I want a full copy of the depedencies as part of the StrangerFlames repo. In the future I'll probably make it be its own repo, though.

Share this post


Link to post
Share on other sites

OK, so there are three things I'm considering now after learning all this:

1. Whenever I make a function, class etc. I put it in the codebase and then in the project I include that file from the codebase.

2. I make all the functions etc. into libraries and include those in whatever projects I need to use them in.

3. I keep all the namespaces, functions, classes etc. in their original project folders and when I need to use a function, class etc. from another project I just include that.

 

Or a fourth option...

Share this post


Link to post
Share on other sites
4. None of the above

Write your project, whatever it may be, as a single project.

Write your next project; when you discover you need functionality from the old project pull it out in to a common folder and reference it there.

To use your structure;

C++
- Project1
- Project2
- Common

Don't just throw things in to common until you need them in more than one place or you'll end up with a mess. Edited by phantom

Share this post


Link to post
Share on other sites

4. None of the above

Write your project, whatever it may be, as a single project.

Write your next project; when you discover you need functionality from the old project pull it out in to a common folder and reference it there.

To use your structure;

C++
- Project1
- Project2
- Common

Don't just throw things in to common until you need them in more than one place or you'll end up with a mess.

 

Thank you! This is the answer I was looking for!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this