Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > Creating DLLs using C++ not C?
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 21st February 10:19
bob watson
External User
 
Posts: 1
Default Creating DLLs using C++ not C?



I've read and got to grips with the answer to an earlier post about writing
a DLL in C for Fortran, but my forte (if forte is the right word for
relative newbie like me) is C++.

There's quite a bit in the example that I saw in the earlier post about C:

Is this the same in C++, and what changes?

Help would be greatly appreciatied

-Bob
  Reply With Quote


 


2 21st February 10:19
alisdair meredith [teamb]
External User
 
Posts: 1
Default Creating DLLs using C++ not C?



C syntax is largely a sub-set of C++ syntax, so often code written in C
is perfectly valid C++. However, sometimes the differences can bite.

For example, C++ allows function overloading, C does not. As the same
function name can now have several sigitures, something has to provide
a hint to the system which particular function you are trying to call.
The accepted solution for this is 'name-mangling' so that the 'real'
name of your function that the linker sees is based on the name you
supplied, along with some hacky symbols that encode the argument names.

Now if you want that function to be exported through a DLL it will
still work, but to import and use it you need to use the mangled name.

The solution built into the language for this problem is to use 'C
linkage' for the function, not C++. That is what the little bit of
pre-processor magic is doing at the top of your header. It spots you
are using a C++ compiler (using a predefined macro required by ISO
standard, so this technique is portable) and declares that all the
following functions are to use the C conventions. They can still be
implemented in C++, using all your favourite C++ features, but can also
safely be called by anything that knows the C calling convention.

AlisdairM(TeamB)
  Reply With Quote
3 21st February 10:19
bob watson
External User
 
Posts: 1
Default Creating DLLs using C++ not C?


Thaks a bundle,
I assume then, that when overloaded functions are linked in, it
determines which one to use the same way C++ compilers do.
The question remains about surpressing those options on the command
line.

"Alisdair Meredith [TeamB]"
  Reply With Quote
4 21st February 10:19
ed mulroy [teamb]
External User
 
Posts: 1
Default Creating DLLs using C++ not C?


There are two names associated with a function, its name in the source
code and its public name. The public name is what is used by the
linker when combining things to form your executable and what is
exported by a DLL and used by the Windows' loader when loading the DLL
and matching the name to that which your program called.

C++ functions can be overloaded, several different functions with the
same source code name who take different sets of calling arguments.
The linker must know which function to match with what call and the
way that is achieved is to have C++ functions named in a way that
encodes the calling argument types. These encoded names are often
called "mangled names". Because of this difference C++ function
public names to differ from those which are C functions.

The extern "C" directive in C++ tells the compiler to form the public
name of what follows in a manner such as is used by C and not as C++.

For example, consider this function:
int FuncName(const char *s, char *t, int u)

As an extern "C" it would have a public name of
_FuncName

As C++ it would have the public name of
@FuncName$qpxcpci

The C++ function remains a C++ function. Only its public name is
effected.

.. Ed
  Reply With Quote


 


Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Development, Ada, Apple script, Assembler, Awk, Beos, Basic, C, C++, C#, C# .net, .net, .net frameworks, Asp .net, Clarion, Clipper, Clos, Clu, Cobol, Coldfusion, Delphi, Dylan, Eiffel, Forth, Fortran, Haskell, Hermes, Icon, Idl, Java, Java script, Jscript .net, Jcl, Linoleum, Lisp, Lotus, Limbo, Logo, Ml, Mumps, Oberon, Postscript, Pop, Pl1, Prolog, Python, Ruby, Pascal, Perl, Php, Rebol, Rexx, Sed, Sather, Scheme, Smalltalk, Tcl, Vhdl, Vrml, Visual basic, Visual basic .net, Yorick, Mysql, Omnis, Postgresql, Xbase, Access, Oracle, Adabas, Berkeley, Btrieve, Filemaker, Gupta, Db2, Informix, Ingres, Mssql server, Object, Olap, Paradox, Rdb, Revelation, Sybase, Theory, Dbase, Html, Java script, Css, Flash, Photoshop, Corel script, Xml, Tech, Beos, Gem, Hp48, Hpux, Linux, Mac, Ms-dos, Os2, Palm, Solaris, Ti99, Windows, Xenix, Aos, Chorus, Geos, Inferno, Lantastic, Lynx, Mach, Minix, Netware, Os9, Parix, Plan9, Psos, Qnx, Xinu, Sco, Unix, Aix, Aux, 386bsd, Bsdi, Freebsd, Netbsd, Openbsd, Ultrix, Amd, Intel, Aptiva, Buz, Deals, Homebuilt, Overclocking, Programming, Extra forums


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666