PDA

View Full Version : C++ Help Required



InvaderGIR
11-04-10, 15:24
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.

ZodiarK
13-04-10, 17:20
i will rumage my brain for the solution:weeee

InvaderGIR
14-04-10, 12:56
I've had quite a few solutions given to me...



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.






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


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:


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 ;
}
]


The first I didn't really understand, the second has resulted in this...




$ 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:

darr
01-05-10, 14:13
You need to define them as extern functions

InvaderGIR
01-05-10, 15:53
:lol:

Slightly late to the game? :p

I've sorted this problem now, I should have said.

Currently the program does what it's meant to do, which is quite handy really. I'm not doing the writeup. I've managed 8k words on the report over the past couple of days, not too bad going really.