473,804 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

name mangling problem

Hi,

I am running into a problem where I am trying to import C DLL into a
Visual C++ 2008 application. The functions in the C DLL are obviously
not name mangled (verified using Dependency Walker). When I try to use
any of the functions from the DLL, VC++ mangles these names (or so it
looks like) and complains that it can't link to mangled versions of
the function names. To avoid this I've figured that I need to declare
the functions I would like to use from the DLL as follows:

extern "C"
{
void Py_Initialize(v oid);
}

Is there any easier way of doing this than to declare all the
functions in the DLL using extern "C"?

Thanks
Jaco

Jul 23 '08 #1
10 4077
On Jul 23, 1:35*pm, Jaco Naude <naude.j...@gma il.comwrote:
Hi,

I am running into a problem where I am trying to import C DLL into a
Visual C++ 2008 application. The functions in the C DLL are obviously
not name mangled (verified using Dependency Walker). When I try to use
any of the functions from the DLL, VC++ mangles these names (or so it
looks like) and complains that it can't link to mangled versions of
the function names. To avoid this I've figured that I need to declare
the functions I would like to use from the DLL as follows:

extern "C"
{
* * void Py_Initialize(v oid);

}

Is there any easier way of doing this than to declare all the
functions in the DLL using extern "C"?

Thanks
Jaco
You could change your C code file extension from .c to .cpp.
But this is not very practical.

Daniel.
Jul 23 '08 #2
Jaco Naude wrote:
Hi,

I am running into a problem where I am trying to import C DLL into a
Visual C++ 2008 application. The functions in the C DLL are obviously
not name mangled (verified using Dependency Walker). When I try to use
any of the functions from the DLL, VC++ mangles these names (or so it
looks like) and complains that it can't link to mangled versions of
the function names. To avoid this I've figured that I need to declare
the functions I would like to use from the DLL as follows:

extern "C"
{
void Py_Initialize(v oid);
}

Is there any easier way of doing this than to declare all the
functions in the DLL using extern "C"?
That's the standard way of informing the C++ compiler functions have C
linkage.

--
Ian Collins.
Jul 23 '08 #3
Jaco Naude <na********@gma il.comwrites:
Hi,

I am running into a problem where I am trying to import C DLL into a
Visual C++ 2008 application. The functions in the C DLL are obviously
not name mangled (verified using Dependency Walker). When I try to use
any of the functions from the DLL, VC++ mangles these names (or so it
looks like) and complains that it can't link to mangled versions of
the function names. To avoid this I've figured that I need to declare
the functions I would like to use from the DLL as follows:

extern "C"
{
void Py_Initialize(v oid);
}

Is there any easier way of doing this than to declare all the
functions in the DLL using extern "C"?
Yes:

#define C(X) extern "C" { X; }

C(void Py_Initialize(v oid))
C(void Py_Middlize(voi d))
C(void Py_Terminalize( void))

Assuming typing 'C( )' is easier than typing 'extern "C"{ }'...

--
__Pascal Bourguignon__
Jul 23 '08 #4
On 2008-07-23 06:35:00 -0400, Jaco Naude <na********@gma il.comsaid:
>
I am running into a problem where I am trying to import C DLL into a
Visual C++ 2008 application. The functions in the C DLL are obviously
not name mangled (verified using Dependency Walker).
Last time I looked, VC++ did mangle names when compiling C code. It
sticks an underscore on the front.
When I try to use
any of the functions from the DLL, VC++ mangles these names
Well, it mangles them differently from the way they're mangled when
they're compiled as C code.
(or so it
looks like) and complains that it can't link to mangled versions of
the function names. To avoid this I've figured that I need to declare
the functions I would like to use from the DLL as follows:

extern "C"
{
void Py_Initialize(v oid);
}

Is there any easier way of doing this than to declare all the
functions in the DLL using extern "C"?
No, that's what extern "C" is for.

Note that this problem really has nothing to do with the use of DLLs,
except that that's where you happened to notice it. It comes up
whenever you need to link C code with C++ code. You have to tell the
compiler that those things were compiled as C so that it can generate
code with the correct calling convention (which can include more than
just changing how the names are mangled).

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jul 23 '08 #5
On Jul 23, 1:00*pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-07-23 06:35:00 -0400, Jaco Naude <naude.j...@gma il.comsaid:
I am running into a problem where I am trying to import C DLL into a
Visual C++ 2008 application. The functions in the C DLL are obviously
not name mangled (verified using Dependency Walker).

Last time I looked, VC++ did mangle names when compiling C code. It
sticks an underscore on the front.
*When I try to use
any of the functions from the DLL, VC++ mangles these names

Well, it mangles them differently from the way they're mangled when
they're compiled as C code.
*(or so it
looks like) and complains that it can't link to mangled versions of
the function names. To avoid this I've figured that I need to declare
the functions I would like to use from the DLL as follows:
extern "C"
{
* * void Py_Initialize(v oid);
}
Is there any easier way of doing this than to declare all the
functions in the DLL using extern "C"?

No, that's what extern "C" is for.

Note that this problem really has nothing to do with the use of DLLs,
except that that's where you happened to notice it. It comes up
whenever you need to link C code with C++ code. You have to tell the
compiler that those things were compiled as C so that it can generate
code with the correct calling convention (which can include more than
just changing how the names are mangled).

--
* Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Thanks for all the replies. Seems like thats the way to do it. I'll go
with the #define C(X) extern "C" { X; } option for now.

Thanks again.
Jul 23 '08 #6
On Jul 23, 12:53 pm, p...@informatim ago.com (Pascal J. Bourguignon)
wrote:
Jaco Naude <naude.j...@gma il.comwrites:
I am running into a problem where I am trying to import C DLL into a
Visual C++ 2008 application. The functions in the C DLL are obviously
not name mangled (verified using Dependency Walker). When I try to use
any of the functions from the DLL, VC++ mangles these names (or so it
looks like) and complains that it can't link to mangled versions of
the function names. To avoid this I've figured that I need to declare
the functions I would like to use from the DLL as follows:
extern "C"
{
void Py_Initialize(v oid);
}
Is there any easier way of doing this than to declare all the
functions in the DLL using extern "C"?
Yes:
#define C(X) extern "C" { X; }
C(void Py_Initialize(v oid))
C(void Py_Middlize(voi d))
C(void Py_Terminalize( void))
Assuming typing 'C( )' is easier than typing 'extern "C"{ }'...
That is, quite frankly, horrible. And what happens if you use C
as a name somewhere? Not to mention that the person reading the
code will have no idea what is going on.

With any decent editor, you can set up an abbreviation so that
you just have to type C to get your extern "C", if it is really
typing which is bothering you. (Although seriously, how long
does it take to type `extern "C"?) More generally, however, the
usual solution is to define an extern "C" block for the entire
library, e.g.:

#ifdef __cplusplus
extern "C" {
#endif
void Py_Initialize( void ) ;
void Py_Middlize( void ) ;
// ...
#ifdef __cplusplus
}
#endif

(I don't normally approve of #ifdef, but this is one of those
cases where it is so ubiquious, any other solution would cause
the reader to wonder why it wasn't done this way.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 23 '08 #7
In article <e6************ *************** *******@y38g200 0hsy.googlegrou ps.com>,
Jaco Naude <na********@gma il.comwrote:
>Thanks for all the replies. Seems like thats the way to do it. I'll go
with the #define C(X) extern "C" { X; } option for now.
If I has no compelling reason to do so, I would not use a macro,
and probably not one named C however appropriate that may seem,
to establish extern "C", but that's just me. It implies a non-C too
(or at least an empty C(X), and I think I'd rather prefer to use
a more traditional "co-ed header" file ala the discussion found at
http://www.comeaucomputing.com/techtalk/#externC ).
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 23 '08 #8
On 23 Temmuz, 13:35, Jaco Naude <naude.j...@gma il.comwrote:
Hi,

I am running into a problem where I am trying to import C DLL into a
Visual C++ 2008 application. The functions in the C DLL are obviously
not name mangled (verified using Dependency Walker). When I try to use
any of the functions from the DLL, VC++ mangles these names (or so it
looks like) and complains that it can't link to mangled versions of
the function names. To avoid this I've figured that I need to declare
the functions I would like to use from the DLL as follows:

extern "C"
{
* * void Py_Initialize(v oid);

}

Is there any easier way of doing this than to declare all the
functions in the DLL using extern "C"?

Thanks
Jaco
Change "Project options" --"Configurat ion Properties" --C/C++ -->
Advanced --Compile as
to "Compile as C Code (/TC)" and remove the " Extern "C" "
declarations
thats all folks ! ;)
No any other changes required...
_______________ ___
http://www.invenoo.com
Jul 24 '08 #9
On Jul 23, 11:36 pm, Jaco Naude <naude.j...@gma il.comwrote:
>
Thanks for all the replies. Seems like thats the way to do it. I'll go
with the #define C(X) extern "C" { X; } option for now.
That's the worst possible solution.

Don't forget you can put multiple prototypes inside a single extern
"C" {} block, so you only have to type it once rather than every time
for that horrible macro. That's how system headers are shared between
C and C++.

Ian.
Jul 24 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2018
by: Steven Bethard | last post by:
Philippe C. Martin wrote: > class Debug_Stderr: > __m_text = '' > __m_log_text = None > __m_dbg = None > __m_refresh_count = 0 <rant> I don't see the benefit in 99.9% of cases for making class variables
1
2526
by: Tim Slattery | last post by:
Does the C++ language standard mandate a particular name-mangling method? I'm trying to use the Entrust toolkit to create a C++ program that encrypts and decrypts files. Entrust supplies header files defining their objects and functions, and *.so files (I'm on a Sun Sparc machine running Solaris) containing the implementation of the functions. I'm compiling with the GNU g++ compiler. According to the
6
6529
by: yyy | last post by:
my question is rather theoretical than practical in the pure programming aspect... is "name mangling" the same as "name decorating" ? browsing the web, you might find multiple people saying "yes", but i vaguely remember having read that it's not exactly like that one might say that "name mangling" is a part of c++ compiler specification and is a naming scheme to support function overloading
4
1451
by: Bruno van Dooren | last post by:
Hi all, i have developed a class that has a member called 'PostMessage' if i only reference this member from within the class context, there is no problem, but if i trie to execute it as a method, i get the error error LNK2019: unresolved external symbol "public: void __thiscall MessageQ::PostMessageA(struct t_Message *)" (?PostMessageA@MessageQ@@QAEXPAUt_Message@@@Z) referenced in function
6
1721
by: David Wade | last post by:
Folks, Does any one know of any "name mangling" software that allows standard C with long names to be linked? Any suggestions for a better place to look? I have tried putting various searchs into google to no avail. Dave.
5
1967
by: Subhransu Sahoo | last post by:
Hi All, Does the C++ standard tell function overloading can't be done with the return types of two functions ? If so, is it true that the name mangling scheme does not take the return type into account ?I know that C++ standard does not tell anything about name mangling and only talks bout function overloading. But, I have observed that the Metrowerks Code Warrior compiler does take return type into the mangling scheme. Is it not a bug...
8
3140
by: sam_cit | last post by:
Hi Everyone, I read somwhere that c++ compiler does name mangling of functions which is why c source code can't invoke functions from object files that were generated using c++ compiler. Can anyone tell in detail as to what name mangling is all about?
4
1974
by: jason.cipriani | last post by:
Does anybody know if C++0x is going to change C++ name mangling at all? Such as, standardizing name mangling instead of leaving it up to the compiler? Jason
14
3002
by: Megalo | last post by:
why not make "name mangling" of C++ standard so should be possible to call the classes and the functions of C++ from other C++ compiler thanks
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10097
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.