LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   how to export a class(g++) (http://www.linuxquestions.org/questions/programming-9/how-to-export-a-class-g-201339/)

onnyloh 07-05-2004 10:14 AM

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

needforspeed 07-05-2004 10:51 AM

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

onnyloh 07-05-2004 11:22 AM

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


stpg 07-06-2004 06:04 AM

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

onnyloh 07-07-2004 06:30 AM

thx it help.


All times are GMT -5. The time now is 07:03 AM.