LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2010, 11:13 AM   #1
FinalDecap
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Rep: Reputation: 0
C++Linking Classes/.cpp files to run one after other?


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:

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, 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.
 
Old 04-11-2010, 11:27 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
You don't want multiple main() functions, you want three functions you can call from main(). Or you could go down a purer OO/Java-style method of giving each class a static main() method, and go through each class calling it.

Either way, you have to do-it-yourself - there are hacks that would allow you to do it in gcc (e.g. using the constructor attribute) but that would wreck portability, and is certainly not what that's designed for.
 
Old 04-12-2010, 02:22 AM   #3
FinalDecap
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by JohnGraham View Post
You don't want multiple main() functions, you want three functions you can call from main(). Or you could go down a purer OO/Java-style method of giving each class a static main() method, and go through each class calling it.
These two options are essentially what I was then planning to do (either of them) but didn't know how to implement either of them...it's been a while since I did any coding and I was never one to be strong on coding practices/OOP which I know is bad and has given me the problem here.

I've had a few other opinions elsewhere and have decided the easiest course of action is for me to create another function/file that contains a main() an calls on the other three.

The idea is that this should be portable between systems with minimal recoding which obviously if I managed to smear them together in some really awkward way then I'd be a bit stuck.

Cheers for the help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
checking how to run the C preprocessor... /lib/cpp bruxelles Linux - Newbie 5 01-19-2008 10:55 AM
KDE g++ program doesn't run test.cpp richikiki Programming 6 03-17-2006 02:47 PM
Including header files and source files for classes Feenix Programming 8 09-28-2005 10:53 AM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM
gcc header files and cpp files? EchO Linux - Software 3 03-01-2005 01:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration