Organizing C++ Code


 
Thread Tools Search this Thread
Top Forums Programming Organizing C++ Code
# 1  
Old 10-05-2011
Organizing C++ Code

I have three directories CspInterp, FpnInterp and LinInterp.
Each directory contains 4 .h and .ccp files describing 4 classes each

CspInterp
class CspFsInterp1d : public FsInterp1d
class CspVsInterp1d : public VsInterp1d
class CspFsInterp2d : public FsInterp2d
class CspVsInterp2d : public VsInterp2d

FpnInterp
class FpnFsInterp1d : public FsInterp1d
class FpnVsInterp1d : public VsInterp1d
class FpnFsInterp2d : public FsInterp2d
class FpnVsInterp2d : public VsInterp2d

LinInterp
class LinFsInterp1d : public FsInterp1d
class LinVsInterp1d : public VsInterp1d
class LinFsInterp2d : public FsInterp2d
class LinVsInterp2d : public VsInterp2d

They are derived from four other classes, each of them also being derived
from two classes

class FsInterp1d : public Interp1d
class VsInterp1d : public Interp1d
class FsInterp2d : public Interp2d
class VsInterp2d : public Interp2d

Each class is coded in its respective .h and .cpp

I need some help on a good way to organize this code for eventually creating a large scale C++ project. I am currently putting things in several directories depending on their topic. The ones shown here are for 1d- and 2d-interpolation using Linear, Fourier Polynomials, and Cubic Splines.
# 2  
Old 10-05-2011
How you organize source files often depends on -- and is constrained by -- how you must compile and link them.

I've seen projects where each little class gets its own separate folder and makefile. These tend to be slow to compile due to all the recursive makes calling makes, and the include paths can be horrendously complex. I don't recommend this model.

On the other end of the scale, I've seen projects where the header files are all lumped in one general include/ folder, and the source files organized by folder. This lets you feed -I ./include into $(CC) and mostly forget about headers. Sometimes you see folders inside the ./include/, and files included like #include <subfolder/subtype.h>. Sometimes you see local .h files in the same source folders as the .c/.cpp files included by #include "localheader.h" alongside header files in a global ./include/ which get included by a #include <globalheader.h> This might be less useful in C++ than C, since C can get away with local-only types and undefined types a lot of times C++ classes can't.

I've also seen projects which are organized into sub-libraries internally, compiled separately and linked in with additions to include and library paths. This is sort of like the first strategy, but with many source files and headers per project instead of few. It helps compartmentalize things while reducing the amount of headaches from enormous path lists and recursing recursion...

It might be useful to really break it up into separate libraries, too. I've often found large applications to be full of things which should have been libraries, being useful in other contexts or easily replacable by standard equivalents, but weren't... Sometimes that's the only thing which makes a 'large' application 'large'...

It's much easier to compartmentalize a C library than a C++ one. It's also much easier to bind C code than C++ code into other languages. This is why you'll often find libraries written in pure C, and C++ template wrappers to turn them into classes for people who want them.

Last edited by Corona688; 10-05-2011 at 01:32 PM..
# 3  
Old 10-05-2011
I do need to separate them into some folders (although not too many) instead of having all the code in just one directory. I would want the .ccp and .h files in the same location as well, rather than having the .h in a separate headers folder. I currently have one makefile.

My only problem is where to put the base classes for the interpolation.

CspInterp
class CspFsInterp1d : public FsInterp1d
class CspVsInterp1d : public VsInterp1d
class CspFsInterp2d : public FsInterp2d
class CspVsInterp2d : public VsInterp2d

FpnInterp
class FpnFsInterp1d : public FsInterp1d
class FpnVsInterp1d : public VsInterp1d
class FpnFsInterp2d : public FsInterp2d
class FpnVsInterp2d : public VsInterp2d

LinInterp
class LinFsInterp1d : public FsInterp1d
class LinVsInterp1d : public VsInterp1d
class LinFsInterp2d : public FsInterp2d
class LinVsInterp2d : public VsInterp2d

Up till here is ok, but then I am left with the following base classes

class FsInterp1d : public Interp1d
class VsInterp1d : public Interp1d
class FsInterp2d : public Interp2d
class VsInterp2d : public Interp2d
class Interp1d
class Interp2d
# 4  
Old 10-05-2011
Quote:
Originally Posted by kristinu
I do need to separate them into some folders (although not too many) instead of having all the code in just one directory. I would want the .ccp and .h files in the same location as well, rather than having the .h in a separate headers folder.

I currently have one makefile.

My only problem is where to put the base classes for the interpolation.
Base headers, at least, should be somewhere generic -- everything needs them. Put them in ./include/, or just leave them in your root folder.

Really though, generic headers don't belong with your source, that's why they're separate files -- for the convenience of other code using it. They need to be easy to find, and traditionally get lumped in one include dir.

If there's bits in your headers that don't make sense for everything to include, separate them out into private header files for your classes, leaving the generic bits to go into ./include/. Only the bits needed to use the class belong in ./include/, things to help define it should be internal.

Of course this all goes out the window if you use templates. Then it's real hard to have an opaque anything.

Last edited by Corona688; 10-05-2011 at 02:23 PM..
# 5  
Old 10-05-2011
I do have some templates as well for performing complex number arithmetic which I have put under directory math.
# 6  
Old 10-05-2011
If a class uses a templated object as any part of its interface, anything that uses the class header needs the template header. So that might as well be in the generic ./include/ too.

That's so you can include <filename.h> without worrying whether it has to be a <../math/filename.h>, <../../../lib/math/filename.h>, <path/to/filename.h>, "/home/username/code/c/project/math/filename.h" or what have you. You'd be surprised at the lengths people go to, to avoid having an organized include folder...

Last edited by Corona688; 10-05-2011 at 03:05 PM..
# 7  
Old 10-05-2011
Do people actually do <path/to/filename.h> when using directory structures to separate the various parts?
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Please help me in organizing the data in UNIX

I have an output file in the form Hostname Value1=abc Value2=def Value3=xyz Hostname1 Value1=abc1 Value2=def1 Value3=xyz1 Hostname2 Value1=abc2 Value2=def2 Value3=xyz2 | | | And so on….. I need to export this output into csv so then it should be in format (8 Replies)
Discussion started by: rahul2662
8 Replies

2. Shell Programming and Scripting

Organizing text file by Capital Names (capital word ' ' capital word)

Hi I have a file passwd_exmpl that contains: root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync... (5 Replies)
Discussion started by: eladage
5 Replies

3. Shell Programming and Scripting

help with organizing some non regular text

hey im trying to get the hex diffrences in two files ones called new and the other is named old i want it to phrase into my script, heres how i need the info: input='\x'94 #the new 1 byte hex change offset=00000000 #the 1st offset of the difference patch unset input offset input='\x'34... (5 Replies)
Discussion started by: lewisdenny
5 Replies

4. Shell Programming and Scripting

Organizing log

Hello, Following are the log of my sms application COMMAND: #tail -30 /var/log/smsd.log | grep Message_id | awk '{print $1,$2,$9}' OUTPUT: 2011-02-21 12:16:20,5, 03218975857, 2011-02-21 12:16:26,5, 03323048252, 2011-02-21 12:16:53,5, 03323048252, 2011-02-21 12:16:59,5,... (1 Reply)
Discussion started by: telnor
1 Replies

5. Shell Programming and Scripting

Organizing Files

Hi, I'm fairly new at scripting. I need to write a script that takes files from a source directory puts them in a target directory and sorts them by artist name. This is what I have so far #!/bin/bash source_dir='/home/tcindy/songs' target_dir='/home/tcindy/music' for path in... (2 Replies)
Discussion started by: tcindy
2 Replies

6. Shell Programming and Scripting

Organizing a log

I have a bunch of log files generated from a shell script, its all of my facebook friends and if theyre logged in. Each file is a different person. It runs every 5 minutes. The log file is just the date and time, then 1 if theyre logged in or 0 if theyre not. part of one of the files is: Mon Aug... (5 Replies)
Discussion started by: killer54291
5 Replies

7. Shell Programming and Scripting

Help organizing a music directory with sub directories.

Hello all, I used to have a great script and I lost it :( What the script did was searched a directory named say "music" It searched all sub directories for .mp3 files Then placeed all the .mp3's into a directory of the band name It also renamed the .mps "track#, band name, album name" (I... (9 Replies)
Discussion started by: komputersman
9 Replies
Login or Register to Ask a Question