LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 07-05-2004, 10:14 AM   #1
onnyloh
Member
 
Registered: Nov 2002
Posts: 57

Rep: Reputation: 15
how to export a class(g++)


example:

#include <iostream>
using namespace std;
class print {
public:
print() {
cout << "Hello World" << endl;
}
};


compiling:
g++ -o libprint.so -shared libprint.cpp

application:
int main() {
extern class print {}; // like this??????
print a; // print Hello World to console
return 0;
}

how to make this worked?
thx for advance
 
Old 07-05-2004, 10:51 AM   #2
needforspeed
Member
 
Registered: Aug 2003
Distribution: Gentoo
Posts: 73

Rep: Reputation: 15
what I have always done is put the class definition is a separate file like you apparently have already called libprint.cpp and then in the main.cpp file, at the top I would #include "libprint.cpp"

Basically, the main.cpp would look like this:
Code:
#include "libprint.cpp"
int main(){
  print InstanceOfPrint;  // declares a variable or instance of the print class
  InstanceOfPrint.print;  // calls the print() method of the print class
}
however, you might consider renaming the method in the libprint file to something else as having a method the same name as the class makes the method the default constructor in this case
 
Old 07-05-2004, 11:22 AM   #3
onnyloh
Member
 
Registered: Nov 2002
Posts: 57

Original Poster
Rep: Reputation: 15
i'm developing a large program.
So shared library should in considered.
Thx for the help by needforspeed


Quote:
Originally posted by needforspeed
what I have always done is put the class definition is a separate file like you apparently have already called libprint.cpp and then in the main.cpp file, at the top I would #include "libprint.cpp"

Basically, the main.cpp would look like this:
Code:
#include "libprint.cpp"
int main(){
  print InstanceOfPrint;  // declares a variable or instance of the print class
  InstanceOfPrint.print;  // calls the print() method of the print class
}
however, you might consider renaming the method in the libprint file to something else as having a method the same name as the class makes the method the default constructor in this case
 
Old 07-06-2004, 06:04 AM   #4
stpg
LQ Newbie
 
Registered: Jun 2004
Distribution: Fedora Core 2
Posts: 11

Rep: Reputation: 0
re

i use smth like that

Code:
// foo/foo.h

class Foo
{
public:
   Foo();
   int bar();
}

// foo/foo.cpp
// compile to libfoo.so
#include "foo.h"
Foo::Foo(){}
int Foo::bar(){return 10}

// main.cpp
// link with libfoo.so (-lfoo)

#include "foo/foo.h"
int main()
{
   Foo foo;
   foo.bar()
}
if you use kdevelop it is easy with automake manager:
1 create subproject foo
2 create library target in subproject foo
3 in main module use -lfoo to use libfoo.so
 
Old 07-07-2004, 06:30 AM   #5
onnyloh
Member
 
Registered: Nov 2002
Posts: 57

Original Poster
Rep: Reputation: 15
thx it help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Implementing a vector class from a list class purefan Programming 9 04-14-2005 10:48 PM
PHP: how to use a method from a class in the same class ldp Programming 5 09-17-2004 09:52 AM
BlackBox.class & VerifierBug.class virus ??? dalek Linux - Security 4 02-29-2004 08:55 AM
Inheriting class members (Qt C++, QApplication class) jtshaw Programming 2 01-15-2004 11:52 AM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 06:33 PM


All times are GMT -5. The time now is 12:09 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration