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.

I have 3x .cpp files which are combined into one using a makefile:

# 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, but I need them to work together as a single application.

I'm doing all my work on Linux using vi to edit the files, but I assume what I want to do is generic between systems.

Any/All help is appreciated.

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

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

Ah, that's awesome, thank you. I'll give it a go in a bit, certainly seems to be what I've been told I should be doing by others thanks to my rubbish coding practices. Doing this has shown me I really need to code sensibly rather than just randomly like I seem to be doing.

Right, I need to get this sorted so have created a new .cpp to try and link them as vijayan121 has suggested.

My interpretation of that is this...

ProgramList.cpp

int ProgramList_main()
{
<body of code>
}

MainFile.cpp

#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...

$ 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 irritated, I meant to have this done by Sunday 11th as I have other things I need to do once this is complete but this is seriously throwing my schedule off.

Anyone able to tell me what I've done wrong here?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.