Results 1 to 7 of 7
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 ...
- 04-11-2010 #1Just Joined!
- Join Date
- Apr 2010
- Posts
- 4
C++ Help Required
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.
- 04-11-2010 #2
Let me get this straight. Are you trying to create a single application using multiple separate class files? Or are you trying to create 3 applications, each of which use the common separate class files?
What you want to do in either case, is produce separate .o output files for each of the separate class source files. Once you have these .o files, you can either handle them individually or put them into a library with the "ar" command or $(AR) in makefiles.
Then you write a separate main() function in a separate file for each application you want to use the common components/classes. Compile the main() source files and then link each individually to the .o files of the common components or to the library you created from them.
There is a lot of detail here left out because makefile can be trivial or non-trivial. It depends on how much you are trying to do.
- 04-11-2010 #3Just Joined!
- Join Date
- Apr 2010
- Posts
- 4
Hi, yeah, I realised after I posted this elsewhere that I hadn't really included enough and forgot to put the extra bits on here.
I think I may have not explained properly...I have 3x .cpp files which are combined into one using a makefile:
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.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
- 04-11-2010 #4
As a reminder, these forums are not to be used for getting homework answers. This sounds precariously like homework.
Assuming the three source files contain some class(es) of use, it is up to your main function to incorporate the three together in some meaningful way. Simply write the main() function to perform the desired sequence of operations using the classes you have compiled. Put this main() function in a separate source file and add it to your makefile just like the others.
The end result would be you have 4 source files to compile and link.
- 04-11-2010 #5Just Joined!
- Join Date
- Apr 2010
- Posts
- 4
Haha, homework. No, this is for my dissertation for uni, quite how it seems like homework without seeing any of the application code is beyond me.
All I'm doing is trying to get opinions on ways in which to achieve compiling into a single application. There is no right or wrong answer to this, it's a matter of opinions and for me to work out which suits me best and implement it as I see fit.
4 source files would work, and thinking about it, is a good way of going about it, I'll give it a go. Cheers.
- 04-11-2010 #6
Part of the problem is that your explanation doesn't seem to fit in with the way that classes and such work.
If ProgramList, MD5Hash, and HazardCheck are all separate classes, then some central "application" code presumably uses them to run. On the other hand, if all of these are separate programs with separate main()s, then they are not classes, and should not be compiled together.
You can think of classes as a bundle of functionality and behaviours, with the main() function being used to actually tie together all of these classes.
So if you want a single virus scanner application using the above three classes, you would normally have a separate scanner.cpp file that contains the main() function and all of the logic of the virus scanner, which uses the classes to help keep various logic encapsulated.
I hope this helps in some way.DISTRO=Arch
Registered Linux User #388732
- 04-11-2010 #7Just Joined!
- Join Date
- Apr 2010
- Posts
- 4
It does help, yeah. Confirms what I've been informed elsewhere...my coding and understanding of classes etc. is rubbish.

Essentially each one is it's own program due to the way I've created them, rather than them being classes. This means I've made a massive headache for myself as I now need to convert them into one application. The reason I originally did the separate programs was due to different compile time variables when testing them as their own modules. What this has then unfortunately done is leave me with 3 sections that I can't easily combine.
It looks like I'm going to have to recode the entire project into one file, this could be an issue due to the fact of different compile time variables being needed for different parts of it.
The reason I assumed it was possible to do it under the way that I have done is due to a tutorial I found for creating make files (can't link due to not enough posts) and me not really paying attention. The files they use obviously have links within them which I've not seen (due to only viewing the make file tutorial page) and so they can be compiled as one application. Also now that it's been mentioned...they have a main.cpp which I assume would contain the main() and would call upon the others as and when needed...if I can work that (as sixdrift mentioned) then that is possibly an easier way of doing it than combining into one .cpp file myself.


Reply With Quote
