C++ library class
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > C++ library class




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    C++ library class  
ioan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-17-07 12:29 PM

//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.






[ Post a follow-up to this message ]



    Re: C++ library class  
Bin Chen


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-17-07 12:29 PM

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.






[ Post a follow-up to this message ]



    Re: C++ library class  
Paul Pluzhnikov


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-17-07 06: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.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:33 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register