C++ Help Required

Soldato
Joined
14 Jul 2005
Posts
17,616
Location
Bristol
This is probably a really stupid thing to ask considering the development I'm doing (effectivly creating a virus scanner), but how do I link classes/cpp files?

I have 3 applications/sections that I can compile/combine with a makefile, that's fine, but I need them to run 1, 2, 3 once the output from the makefile is done.

Currently the only section to actually run is whichever I have "main" in and obviously if I put that into all three, they won't compile as one.

I've been looking all over the place at all sorts, header files and such, but there is no mention of how to actually do this although I'm sure it must be possible. I'm used to being able to do this in Java and I'm sure I've seen C++ applications do it, but not worked out how.

Any/All help is appreciated.
 
all: bin1 bin2 bin3

bin1: sources main1
bin2: sources main2
bin3: sources main3

The important point is that the all target isn't attempting to link any binaries.
 
I think I may have not explained properly...I have 3x .cpp files which are combined into one using a makefile:

Code:
# Virus Scanner

scanner.out : ProgramList.o MD5Hash.o HazardCheck.o
	g++ -o scanner.out ProgramList.o MD5Hash.o HazardCheck.o /usr/lib/libstdc++.so.5 CONFIG/rudeconfig.lib HASH/src/libhl++.a

ProgramList.o: PROGRAMS/ProgramList.cpp
	g++ -c PROGRAMS/ProgramList.cpp

MD5Hash.o: HASH/MD5Hash.cpp
	g++ -c HASH/MD5Hash.cpp

HazardCheck.o: CONFIG/HazardCheck.cpp CONFIG/hazard.conf
	g++ -c CONFIG/HazardCheck.cpp 

clean:
	rm *.o scanner.out

#END OF MAKE FILE

scanner.out will run, but only whichever .cpp has "main()" in it, so the other two are defunct but are included within scanner.out. If I compile the three files separately I end up with three programs, I need them to work together as a single application.

I'm not too sure quite what you mean though.
 
I need it to be a single application rather than 3 separate ones, so whilst that would work, it's not ideal at all...I think my dissertation tutor would kill me if I submitted my program as a script. :p
 
Why arn't you just instantiating your classes from the main function?

An app can only have one 'main'.

Oh and at work we've exceeded the max path length limit on our compiler, some 32,767 characters.
 
After some beer I think, young padewan, you have some understanding of why you should be beaten into making a well shaped program.. like a bonsai tree.. (ok I've been drinking and watching a Ninja film..).
 
Is your scanner a resident (ie: service) program or an executable that runs through the hard disk?
 
Why arn't you just instantiating your classes from the main function?

An app can only have one 'main'.

I know it can only have one main...hence the problem I ended up with by writing three separate programs effectively for different aspects. :o

From your post and from one on another forum where I got told I was 'trying to do my homework and want and want the internet to do it for me' I realise I should should do this, either as a 4th one calling the other 3 or as each one calling the next...problem is, it's not something I've done in C++, I've done it in Java, but about 2 years ago and honestly can't recall how you're meant to do it...else I'd have been trying that. :p

EDIT: I think the reason I did it like this is simply because that's essentially how I was told to do it in Java, using multiple classes etc. But I can't remember properly as it's been 2 years and I've not coded much in that time.

Is your scanner a resident (ie: service) program or an executable that runs through the hard disk?

It is an executable that checks memory in use for any threats. :)

After some beer I think, young padewan, you have some understanding of why you should be beaten into making a well shaped program.. like a bonsai tree.. (ok I've been drinking and watching a Ninja film..).

What? :p
 
Last edited:
Just as a matter of interest, I wrote an AV scanner many years ago under DOS using TurboC and Turbo Assembler. Basically it was one big recursive loop that worked its way through the local drive finding every file and then loading it into RAM and doing a bunch of strcmps on the target file and a virus string database I'd built up.

This was a long time ago mind. Got me a job at Symantec for my troubles. :)
 
Well I just totally failed trying to make it one big file. :(

I've been out of coding for too long, I just don't get what I'm doing...I managed the three different programs fine, but the problem is I was never strong at multiple classes or files in Java and it seems that trait is with C++ too. :mad: :(
 
They should be separate files.

Most .cpp files don't represent runnable programs, just units of code that are intended to be used from elsewhere. To use it, you must make the compiler aware of its interface beforehand (#include the header file) and tell your build system where to find its actual implementation (i.e. the .o listed in your Makefile).
 
The simple idea would be to make each program main() into an class then rename main() as run() within that class.

ProgramList - the main() becomes ProgramList::run().
MD5Hash - the main() becomes MD5Hash::run().
HazardCheck - the main() becomes HazardCheck::run().

Then create a new main():
int main(int argc, char *argv[]) {
ProgramList programList;
MD5Hash hash;
HazardCheck hazardCheck;

programList.run();
hash.run();
hazardCheck:run();
}

Or something like that.
 
The simple idea would be to make each program main() into an class then rename main() as run() within that class.

ProgramList - the main() becomes ProgramList::run().
MD5Hash - the main() becomes MD5Hash::run().
HazardCheck - the main() becomes HazardCheck::run().

Then create a new main():
int main(int argc, char *argv[]) {
ProgramList programList;
MD5Hash hash;
HazardCheck hazardCheck;

programList.run();
hash.run();
hazardCheck:run();
}

Or something like that.

Certainly is along the lines of what I've been told elsewhere, that should do it and I'll report back just how badly I mess it up. :p

Thanks for the help guys...I realise now just how poor my programming practices are and I need to drastically improve them. :)
 
Ok so I wasn't sure how to implement what Nick has said so I figured I'd go down the route of something I understood a bit more...


Write three 'main' functions, one in each file.

Code:
int ProgramList_main( int argc, char** argv ) ;
int MD5Hash_main( int argc, char** argv ) ;
int HazardCheck_main( int argc, char** argv ) ;

Then write a single real main function, which calls these one after another:

Code:
int main( int argc, char** argv )
{
   int exit_code = ProgramList_main( argc, argv ) ;
   if( exit_code == 0 ) exit_code = MD5Hash_main( argc, argv ) ;
   if( exit_code == 0 ) exit_code = HazardCheck_main( argc, argv ) ;
   return exit_code ;
}


So my interpretation of that is this...

ProgramList.cpp
Code:
int ProgramList_main()
{
<body of code>
}

MainFile.cpp
Code:
#include "PROGRAMS/ProgramList.cpp"
#include "HASH/MD5Hash.cpp"
#include "CONFIG/HazardCheck.cpp"

int main( int argc, char** argv )
{
   int exit_code = ProgramList_main() ;
   if( exit_code == 0 ) exit_code = MD5Hash_main() ;
   if( exit_code == 0 ) exit_code = HazardCheck_main() ;
   return exit_code ;
}



Which when compiling has resulted in this...


Code:
$ make -f makefile
g++ -c MainFile.cpp
g++ -c PROGRAMS/ProgramList.cpp
g++ -c HASH/MD5Hash.cpp
g++ -c CONFIG/HazardCheck.cpp
g++ -o scanner.out MainFile.o ProgramList.o MD5Hash.o HazardCheck.o /usr/lib/libstdc++.so.5 CONFIG/rudeconfig.lib HASH/src/libhl++.a
ProgramList.o: In function `ProgramList_main()':
ProgramList.cpp:(.text+0x5c): multiple definition of `ProgramList_main()'
MainFile.o:MainFile.cpp:(.text+0x100e): first defined here
MD5Hash.o: In function `MD5Hash_main()':
MD5Hash.cpp:(.text+0x5c): multiple definition of `MD5Hash_main()'
MainFile.o:MainFile.cpp:(.text+0x890): first defined here
HazardCheck.o: In function `HazardCheck_main()':
HazardCheck.cpp:(.text+0x5c): multiple definition of `HazardCheck_main()'
MainFile.o:MainFile.cpp:(.text+0x5c): first defined here
collect2: ld returned 1 exit status
make: *** [scanner.out] Error 1


With me just getting :mad:
 
Dude, you're trying to run before you know how to walk.

Find a copy of book, read a couple of sections on declarations-vs-definitions & the purpose of #include and header files (.h). Then fixing this will be enjoyable rather than a trial and error :mad:fest.
 
Call all 3 from main() in the order you want??? then they can all be spread out wherever you like? (include all 3 at the top of file containing main)


edit: ahh, should have read the thread ignore
 
Back
Top Bottom