Organizing C++ Code


 
Thread Tools Search this Thread
Top Forums Programming Organizing C++ Code
# 8  
Old 10-05-2011
I've seen people make sub-directories inside ./include, so you can do #include <math/template.h> instead of mashing them all together in ./include. You get to organize the headers by folder that way, and it's still <math/template.h> everywhere if you do -I ./include properly.

The rest were examples of what people end up having to do if they refuse to have a proper folder for headers. That, or putting in 97 different -I directives to include each and every project in the search path.

I suppose you could do -I./, and then you'd be able to do #include <math/template.h> just by having a folder named 'math' in your source root. That could get awkward if you start getting libraries nested inside libraries though.
# 9  
Old 10-05-2011
In most of my classes I have both a .h and a .cpp, with some inline functions in the .h file as well. One reason I was thinking of keeping the .cpp and corresponding .h files together in same directory. For templates, the declaration and implementation are all in one file as usual.

---------- Post updated at 01:39 PM ---------- Previous update was at 01:26 PM ----------

I had something like this

Code:
#
#  TOMMY - Makefile

#  -History---------------------------------------------------------------
#
#  V01 - DEC 1999 - Luca D'Auria
#        Initial release.
#  V02 - SEP 2008 - Luca Elia
#        Version fixed for g++ 4.3.0.
#        The following settings make it compile without warnings using:
#           g++ (GCC) 3.4.5 (mingw-vista special r3)
#           g++ (GCC) 4.0.2 20051125 (Red Hat 4.0.2-8)
#           g++ (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
#           g++ (GCC) 4.3.0 20080305 (alpha-testing) mingw-20080502
#        The -enable-auto-import bit is needed on some newer gcc version.
#        Enable optimizations and disable debug checks so that the final
#        executable can run faster (up to twice as fast).
#  V03 - NOV 2008 - Christopher Dimech
#        Code restructuring.
#
#  -----------------------------------------------------------------------

# GNU Compiler flags

C++ = g++
OPT = -traditional -Wno-non-template-friend -Wno-deprecated -O3 -DNDEBUG
DEF = -DREAL=float -DGetREAL=Getfloat

# OPT = -traditional -Wno-non-template-friend -Wno-deprecated -O3 -DNDEBUG
#       -enable-auto-import

# DEF = -DREAL=double -DGetREAL=Getdouble

# Directories

B = ./baseLib
T = ./tommy
O = ./others
IDIR = -IbaseLib -Itommy -Iothers

all : raytrac tdarwin tsimplex getvel getpdf getmisf tracepdf

# TOMMY files

raytrac : $(T)/RayTrac.cc baselib tommylib
    $(C++) $(OPT) $(DEF) $(IDIR) $(T)/RayTrac.cc -o raytrac
    -mv raytrac ./bin

tdarwin : $(T)/Tdarwin.cc baselib tommylib
      $(C++) $(OPT) $(DEF) $(IDIR) $(T)/Tdarwin.cc -o tdarwin
    -mv tdarwin ./bin

tsimplex : $(T)/Tsimplex.cc baselib tommylib
    $(C++) $(OPT) $(DEF) $(IDIR) $(T)/Tsimplex.cc -o tsimplex
    -mv tsimplex ./bin

getvel : $(T)/Getvel.cc baselib tommylib
    $(C++) $(OPT) $(DEF) $(IDIR) $(T)/Getvel.cc -o getvel
    -mv getvel ./bin

getpdf : $(T)/GetPDF.cc baselib tommylib
    $(C++) $(OPT) $(DEF) $(IDIR) $(T)/GetPDF.cc -o getpdf
    -mv getpdf ./bin

getmisf : $(T)/Getmisf.cc baselib tommylib
    $(C++) $(OPT) $(DEF) $(IDIR) $(T)/Getmisf.cc -o getmisf
    -mv getmisf ./bin

rdt : $(T)/Rdt.cc
    $(C++) $(OPT) $(DEF) $(IDIR) $(T)/Rdt.cc -o rdt
    -mv rdt ./bin

tracepdf : $(T)/TracePDF.cc baselib tommylib
    $(C++) $(OPT) $(DEF) $(IDIR) $(T)/TracePDF.cc -o tracepdf
    -mv tracepdf ./bin

# Libraries

baselib : $(B)/vect.h $(B)/vector.h $(B)/matrix.h $(B)/dynstr.h $(B)/sstring.h

tommylib : $(T)/layer.h $(T)/linlay.h $(T)/velmod.h

##########################################################################

# 10  
Old 10-05-2011
Quote:
Originally Posted by kristinu
In most of my classes I have both a .h and a .cpp, with some inline functions in the .h file as well. One reason I was thinking of keeping the .cpp and corresponding .h files together in same directory. For templates, the declaration and implementation are all in one file as usual.
I've already spent this entire thread explaining that this is not "as usual". Please reread.

IF your entire project is two or three files, why bother making folders at all? #include "filename.h" is easy. But when you divide things into multiple folders, #include "filename.h" doesn't work -- they can't find each others' headers, anymore. The code or the compiler needs to be told where to look. Lots of ways to do that:

1) #include "../library1/filename.h" -- silly.
2) cc -I ./library1 -I ./library2 -I ./library3 -I ./library4 -I ./library5 -I ./library6 -- I bet you can see the problem with this too.
4) Create symlinks in each and every individual folder so #include "filename.h" succeeds -- a silly amount of work.
3) cc -I ./include -- the way it's supposed to work.

Header files are supposed to be grouped separately from the source. They're supposed to be separate enough that, once you compile the .cpp file, you can tar up and ship the .o or .so with the .h file and forget the .cpp. That's why they exist -- to declare what symbols are available in an object or library without defining the object's entire contents.
# 11  
Old 10-05-2011
Yep, I've been following your comments
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