|
Home > Archive > Unix Programming > March 2007 > C++ library class
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| //A.h
#ifndef H_A_H
#define H_A_H
class A {
public:
int SecretMessage ();
};
#endif
// A.cc
int A :: Message () {
return 6001;
}
// Client.cc
#include "./A.h"
#include <iostream>
using namespace std;
int main () {
A a;
cout << a.SecretMessage () << endl;
return 0;
}
// end of files
###########################
# Compile & Use #
###########################
$ g++ -g -shared A.cc -o libA.so
$ g++ -g -L. -lA Client.cc -o Client
$ ./Client
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$ Motivation $
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
I tried to export "classes" from so (shared objects),
but as a newby to gcc compiler tools I made mistakes.
Here is what I learn and I'd like to share:
1. libA.so is important to have "lib" suffix at libA.so build time.
2. Client code is linked with libA.so using -L<path to libA.so> -lA
Notice "libA.so" --> "A" at build time for Client code.
Happy coding.
| |
| Bin Chen 2007-03-17, 7:29 am |
| On Mar 17, 4:18 pm, "ioan" <niciodata...@gmail.com> wrote:
> //A.h
> #ifndef H_A_H
> #define H_A_H
> class A {
> public:
> int SecretMessage ();};
>
> #endif
> // A.cc
> int A :: Message () {
> return 6001;}
>
> // Client.cc
> #include "./A.h"
>
> #include <iostream>
> using namespace std;
>
> int main () {
> A a;
> cout << a.SecretMessage () << endl;
> return 0;}
>
> // end of files
>
> ###########################
> # Compile & Use #
> ###########################
> $ g++ -g -shared A.cc -o libA.so
> $ g++ -g -L. -lA Client.cc -o Client
> $ ./Client
>
> $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
> $ Motivation $
> $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
>
> I tried to export "classes" from so (shared objects),
> but as a newby to gcc compiler tools I made mistakes.
> Here is what I learn and I'd like to share:
> 1. libA.so is important to have "lib" suffix at libA.so build time.
> 2. Client code is linked with libA.so using -L<path to libA.so> -lA
> Notice "libA.so" --> "A" at build time for Client code.
>
> Happy coding.
Good, but this is not a group for C++, this is for unix. I can't
figure whats your post has any relation to this topic.
| |
| Paul Pluzhnikov 2007-03-17, 1:22 pm |
| "ioan" <niciodata.eu@gmail.com> writes:
> //A.h
> #ifndef H_A_H
> #define H_A_H
> class A {
> public:
> int SecretMessage ();
> };
> #endif
> // A.cc
> int A :: Message () {
> return 6001;
> }
Code above will not compile, since header says there should be
A::SecretMessage(), but source defines A::Message().
Lesson: whenever posting code, post *actual* code, not your
'recollection' of it.
> $ g++ -g -L. -lA Client.cc -o Client
This command line is wrong. Correct command line is something like:
g++ -g Client.cc -o Client -L. -lA
To understand why order of sources and libraries on command line
matters, read this: http://webpages.charter.net/ppluzhnikov/linker.html
> Here is what I learn and I'd like to share:
This is all trivial stuff. Are you going to 'share' that
2+2 == 4 and 2+3 == 5 next?
Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
|
|
|
|
|