🔒 Locked
C++ name mangling
Advertisement
Original Post
Because of different name-mangling-shemas of different compilers nobody can be sure that the function: void Add(∫, ∫) has the equivalent string name: "Add". And i want to get the name of a method of a class(for example void CWindow::CreateWindow()). Is there a way to get the c++ name-magled name of a function into a TextFile or into a string or char* or into a binary file(getting into a char* will be enough ;). There is no problem to save the char* into a text or binary file later on)???
Hello Songoku,
? why do you want to save the mangling names?
You should be able to was the linker form your compiler or system to output names of functions and file level vars contained in a library or object file.
Since it is the library program that packages obj file into library it also should list out what is in them.
Not sure what the library program is called on windows.
For static member you can extern "C" it also as long as you don't have another function the same name.
exten "C" static void Add(int, int);
Lord Bart :)
? why do you want to save the mangling names?
You should be able to was the linker form your compiler or system to output names of functions and file level vars contained in a library or object file.
Since it is the library program that packages obj file into library it also should list out what is in them.
Not sure what the library program is called on windows.
For static member you can extern "C" it also as long as you don't have another function the same name.
exten "C" static void Add(int, int);
Lord Bart :)
Hello Songoku,
Ok, I understand, but I will point out something to you about use code compiler by different vendors compilers.
The name mangling can be different but also the calling convertion, class/struct layout.
So even if you get the mangled name you still might have problems use it.
Because it up to the on vendor how they do there classes.
prime example is the virtaul function tables.
This is very important thing to understand.
Lord Bart :)
Ok, I understand, but I will point out something to you about use code compiler by different vendors compilers.
The name mangling can be different but also the calling convertion, class/struct layout.
So even if you get the mangled name you still might have problems use it.
Because it up to the on vendor how they do there classes.
prime example is the virtaul function tables.
This is very important thing to understand.
Lord Bart :)
i have thougth long about this think. And i have a good solution.
my lient app has an pointer to an interface class. The dll inherits from this class. So i have no problems with virtual function tables because every compiler has to have equivalent virtual function tables for pure virtual functions inside a class or interface classes. i read an article about this. But i do not want to have a global function in an dll because i want to be as much OO as possible and if it is possible to get the mangled name i could program with every envirement and my client app would have no problems.
my lient app has an pointer to an interface class. The dll inherits from this class. So i have no problems with virtual function tables because every compiler has to have equivalent virtual function tables for pure virtual functions inside a class or interface classes. i read an article about this. But i do not want to have a global function in an dll because i want to be as much OO as possible and if it is possible to get the mangled name i could program with every envirement and my client app would have no problems.
You can always load the dll and call getprocaddress with the undecorated name. That's always guarenteed to work. Or if you want to link in a dll built with another compiler you can use a defs file.
Those two options should cover most cases. Do you have another case where this wouldn't work??
Cheers
Chris
Those two options should cover most cases. Do you have another case where this wouldn't work??
Cheers
Chris
All in all: a bad idea.
Your program wouldn't survive moving to another compiler, and might not even survive upgrading to another version of your current compiler.
There is no portable way to automatically determine the mangled name of a function within C++ itself.
I'd make an extern "C" wrapper around A::B. You might call it "A_B", for example.
Your program wouldn't survive moving to another compiler, and might not even survive upgrading to another version of your current compiler.
There is no portable way to automatically determine the mangled name of a function within C++ itself.
I'd make an extern "C" wrapper around A::B. You might call it "A_B", for example.
Ok you have a class that has a pointer to the actual class and you only call the interface methods, which into in the dll code call the imp class member function.
Thats fine, but you need to make sure that params and return things are compatable between, if one compile doesn't pad and the other did you have a problem.
If any data members are base off of size that a compiler can set then you got a problem.
ex:
size_t is compiler defined sometimes.
bool is compiler defined all the time.
if compiler A has bool is one byte and compiler B has bool is 4 bytes then you have big problem.
Lord Bart :)
Thats fine, but you need to make sure that params and return things are compatable between, if one compile doesn't pad and the other did you have a problem.
If any data members are base off of size that a compiler can set then you got a problem.
ex:
size_t is compiler defined sometimes.
bool is compiler defined all the time.
if compiler A has bool is one byte and compiler B has bool is 4 bytes then you have big problem.
Lord Bart :)
if it does not work i can only export a global function to the dll and inport this
function via getprocaddress to create an object of the derived class. So everything is going very well. but i searched a way of getting this function into the class implementation to be OO. But it seems not to work.
function via getprocaddress to create an object of the derived class. So everything is going very well. but i searched a way of getting this function into the class implementation to be OO. But it seems not to work.
Hello Songoku,
If you truly want to use classes compile by one complier with another then you need to follow a few steps.
1) make sure data members are compiler netural in size as possible.
ie. try to avoid bool and size_t. Unless you veryified there the same size on both compilers.
2) try to make sure same calling convection are used.
3) warp class methods in C callable code.
4) Do not create any classes/structure use in dll on your own by new or on the stack.
Use only a Create method from the dll to return you a pointer to class/structure.
This assure you the class is created properly by the compler code that controls the class ie the dll in question.
This way you can still hold pointers to class from the other compiler and if you want to acces a method you call C funtion with passed in pointer of class.
There is a reason why most of windows API is C callabe so other compilers can use them.
Lord Bart :)
If you truly want to use classes compile by one complier with another then you need to follow a few steps.
1) make sure data members are compiler netural in size as possible.
ie. try to avoid bool and size_t. Unless you veryified there the same size on both compilers.
2) try to make sure same calling convection are used.
3) warp class methods in C callable code.
4) Do not create any classes/structure use in dll on your own by new or on the stack.
Use only a Create method from the dll to return you a pointer to class/structure.
This assure you the class is created properly by the compler code that controls the class ie the dll in question.
This way you can still hold pointers to class from the other compiler and if you want to acces a method you call C funtion with passed in pointer of class.
There is a reason why most of windows API is C callabe so other compilers can use them.
Lord Bart :)
Quote:
Original post by antareus Quote:
Original post by Songoku
class implementation to be OO. But it seems not to work.
Global functions are merely static member functions, except not within a class.
That's not entirely true, though. A static member function has access to private members of its class, both static and non-static.
Whether that's desirable or not is another discussion...
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement