Linux-Bulgaria.ORG
навигация

 

начало

пощенски списък

архив на групата

семинари ...

документи

как да ...

 

 

Предишно писмо Следващо писмо Предишно по тема Следващо по тема По Дата По тема (thread)

Re: lug-bg: c/c++ differences


  • Subject: Re: lug-bg: c/c++ differences
  • From: firedust@email.domain.hidden (Stanislav Lechev [AngelFire])
  • Date: Mon, 7 Jul 2003 15:49:53 +0300


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

<p>10x za primera ... wypreki che komentara mi se obezmislq malko
no wse pak shte go napisha...
stawashe duma che nqma kak da prehwyrlish name mangling-a w C
qwno ne sym se izrazil prawilno ili prosto ne si me razbral...

primerno da si naprawish C++ modul
w kojto da imash
bool testfunc (int a);
bool testfunc (long a);

i posle da widim dlsym-a koe shte ti izkara. ..
w nqkoe ot prednite pisma nqkoj beshe pratil
url s howto ... (http://www.tldp.org/HOWTO/mini/C++-dlopen/)
w koeto dave e napisano :

- ---
Because C++ allows overloading (different functions with the same name but 
different arguments) and has many features C does not - like classes, member 
functions, exception specifications - it is not possible to simply use the 
function name as the symbol name. To solve that, C++ uses so-called name 
mangling, which transforms the function name and all the necessary 
information (like the number and size of the arguments) into some 
weird-looking string which only the compiler knows about. The mangled name of 
foo might look like foo_at_4%6^, for example. Or it might not even contain the 
word "foo".
One of the problems with name mangling is that the C++ standard (currently 
[ISO14882]) does not define how names have to be mangled; thus every compiler 
mangles names in its own way. Some compilers even change their name mangling 
algorithm between different versions (notably g++ 2.x and 3.x). Even if you 
worked out how your particular compiler mangles names (and would thus be able 
to load functions via dlsym), this would most probably work with your 
compiler only, and might already be broken with the next version.

C++ has a special keyword to declare a function with C bindings: extern "C". A 
function declared as extern "C" uses the function name as symbol name, just 
as a C function. For that reason, only non-member functions can be declared 
as extern "C", and they cannot be overloaded.
- ---

On Friday 04 July 2003 18:43, Nikolay Mitev wrote:
<em class="quotelev1">> Hi,
<em class="quotelev1">>
<em class="quotelev2">> > i wse pak reshenie imalo za koeto ne se bqh setil
<em class="quotelev2">> > extern "C"
<em class="quotelev2">> > samo deto trqbwa da poexperimentiram s towa
<em class="quotelev2">> > zashtoto ne sym siguren kakwo shte se poluchi
<em class="quotelev2">> > s polimorfizma :)
<em class="quotelev2">> > 10x za otgowora wse pak :)
<em class="quotelev1">>
<em class="quotelev1">> kakuv polimorphism te goni be 4ovek? Nqma takuv film tuka. Ima6 ptr kum
<em class="quotelev1">> funkciq. Nqma6 obekt, nqma6 ni6to.  Kakuv polymorphism bez obekt?  Eti te
<em class="quotelev1">> elegantno re6enie na problema ti s polymorphism.
<em class="quotelev1">>
<em class="quotelev1">> main program:
<em class="quotelev1">>
<em class="quotelev1">> obtest.h
<em class="quotelev1">> ======
<em class="quotelev1">>
<em class="quotelev1">> class Test {
<em class="quotelev1">>  public:
<em class="quotelev1">>   virtual void do_something () const {
<em class="quotelev1">>     std::cout << "Shit, no polymorphism :-(" << std::endl;
<em class="quotelev1">>   }
<em class="quotelev1">> };
<em class="quotelev1">>
<em class="quotelev1">> ======
<em class="quotelev1">>
<em class="quotelev1">> main.cpp
<em class="quotelev1">>
<em class="quotelev1">> ======
<em class="quotelev1">> #include <iostream>
<em class="quotelev1">> #include <dlfcn.h>
<em class="quotelev1">>
<em class="quotelev1">> #include "obtest.h"
<em class="quotelev1">>
<em class="quotelev1">> int main () {
<em class="quotelev1">>
<em class="quotelev1">>   void *d = dlopen ("libtest.so", RTLD_NOW);
<em class="quotelev1">>
<em class="quotelev1">>   if (d == 0) {
<em class="quotelev1">>     std::cout << "Can't find library" << std::endl;
<em class="quotelev1">>   }
<em class="quotelev1">>
<em class="quotelev1">>   Test* (*object_factory) () = (Test* (*) ()) dlsym (d, "object_factory");
<em class="quotelev1">>   Test* t;
<em class="quotelev1">>   if (object_factory != 0) {
<em class="quotelev1">>     t = object_factory ();
<em class="quotelev1">>   } else {
<em class="quotelev1">>     std::cout << "Can't find function" << std::endl;
<em class="quotelev1">>     exit (1);
<em class="quotelev1">>   }
<em class="quotelev1">>
<em class="quotelev1">>   t->do_something ();
<em class="quotelev1">> }
<em class="quotelev1">>
<em class="quotelev1">> ======
<em class="quotelev1">>
<em class="quotelev1">> plugin:
<em class="quotelev1">>
<em class="quotelev1">> test.cpp
<em class="quotelev1">>
<em class="quotelev1">> ======
<em class="quotelev1">>
<em class="quotelev1">> #include <iostream>
<em class="quotelev1">>
<em class="quotelev1">> #include "obtest.h"
<em class="quotelev1">>
<em class="quotelev1">> class Test1 : public Test {
<em class="quotelev1">>
<em class="quotelev1">> public:
<em class="quotelev1">>
<em class="quotelev1">>   virtual void do_something () const {
<em class="quotelev1">>     std::cout << "Wow, polymorphism" << std::endl;
<em class="quotelev1">>   }
<em class="quotelev1">> };
<em class="quotelev1">>
<em class="quotelev1">> static Test1 test;
<em class="quotelev1">>
<em class="quotelev1">> extern "C" {
<em class="quotelev1">>   Test* object_factory () {
<em class="quotelev1">>     return &test;
<em class="quotelev1">>   }
<em class="quotelev1">> }
<em class="quotelev1">>
<em class="quotelev1">> ======
<em class="quotelev1">>
<em class="quotelev1">> test.cpp kompilira6 s g++ -shared -olibtest.so test.cpp
<em class="quotelev1">>
<em class="quotelev1">> drugoto g++ -otest main.cpp i runva6.
<em class="quotelev1">>
<em class="quotelev1">> Kratko obqsnenie.:
<em class="quotelev1">> Izpolzva6 plugin-a kato faktory za obekti. Func-a ti vru6ta ptr KUM OBEKT.
<em class="quotelev1">> Prez nego kato vikne6 funkciq ima6 ve4e polymorphism v deijstvie.
<em class="quotelev1">>
<em class="quotelev1">> N.B. Naro4no test v plugina e static, za6toto ne e hubavo da se zadelq
<em class="quotelev1">> pamet v dll i da se osvobozhdava v main, naprimer, za6toto mozhe dll-a i
<em class="quotelev1">> app-a da izpolzvat razli4ni heap-ove i da stane boza. Zatova ako zadelq6 v
<em class="quotelev1">> factory-to dinami4no, napravi i o6te edna func destroy (void *), naprimer,
<em class="quotelev1">> v plugin-a, s koqto da osvobozhdava6 zadelenata pamet pak v konteksta na
<em class="quotelev1">> plugin-a.
<em class="quotelev1">>
<em class="quotelev1">> E, dano sum pomognal.
<em class="quotelev1">>
<em class="quotelev1">> cheers,
<em class="quotelev1">> face

- -- 
- -=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-
  Regards,                                            AngelFire  
      Stanislav Lechev                    <firedust_at_vega.bg>     
     PGP Key: http://firedust.vega.bg/pgp/StanislavLechev.asc    
   Vega Internet Service Provider (tm)  --  http://www.vega.bg   
- -=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-=#=-

<p>-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE/CWxx8RPXBhiMqewRAnZcAJ0YLkesbmTy47gMppiAZ5oe6+dUfQCeN/s+
RT9n7gIythaaXbCMMmBJRA8=
=dwzQ
-----END PGP SIGNATURE-----
============================================================================
A mail-list of Linux Users Group - Bulgaria (bulgarian linuxers).
http://www.linux-bulgaria.org - Hosted by Internet Group Ltd. - Stara Zagora
To unsubscribe: http://www.linux-bulgaria.org/public/mail_list.html
============================================================================



 

наши приятели

 

линукс за българи
http://linux-bg.org

FSA-BG
http://fsa-bg.org

OpenFest
http://openfest.org

FreeBSD BG
http://bg-freebsd.org

KDE-BG
http://kde.fsa-bg.org/

Gnome-BG
http://gnome.cult.bg/

проект OpenFMI
http://openfmi.net

NetField Forum
http://netField.ludost.net/forum/

 

 

Linux-Bulgaria.ORG

Mailing list messages are © Copyright their authors.