Way to convert Macro Defined functions from linux Kernel in Visual C++
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Way to convert Macro Defined functions from linux Kernel in Visual C++
Hello everyone,
I am dealing with some Linux kernel code, which define most its functions with Macros. The trouble I am having is to use those code in user space in Windows.
For example, I have a function defined like this:
-------------------------------------------------------------------------
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_entry((head)->next, typeid(*pos), member), \
n = list_entry(pos->member.next, typeid(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeid(*n), member))
For example, I have a function defined like this:
-------------------------------------------------------------------------
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_entry((head)->next, typeid(*pos), member),
<SNIP>
It seems you replaced the linux macro definition by substituting typeid for typeof. You cannot do this!
AFAIK (and my knowledge on VC) is quite limited, typeid != typeof. The typeid construct is part of the RTTI of ISO C++. The typeof construct is not ISO C or ISO C++, and it cannot be implemented portably on any given compiler. If you want to use it on windows, I suggest using mingwin-gcc or icc, as both of these are known to have a version of typeof compatible with that used in linux.
If your goal is to use this sort of code in userspace, I would simply recommend using C++ (and its template functionality will take care of this). You might instead try a hack that will create a macro for typeof by relying on other nonportable features of VC(++) as found here.
Last edited by osor; 06-28-2009 at 09:20 PM.
Reason: clarification
looks like the '\' at the end of each lines is not compatible in VC, but the site you refer to is using this, weird, right ?
Quote:
Originally Posted by osor
It seems you replaced the linux macro definition by substituting typeid for typeof.
AFAIK (and my knowledge on VC) is quite limited, typeid != typeof. The typeid construct is part of the RTTI of ISO C++. The typeof construct is not ISO C or ISO C++, and it cannot be implemented portably on any given compiler. If you want to use it on windows, I suggest using mingwin-gcc or icc, as both of these are known to have a version of typeof compatible with that used in linux.
If your goal is to use this sort of code in userspace, I would simply recommend using C++ (and its template functionality will take care of this). You might instead try a hack that will create a macro for typeof by relying on other nonportable features of VC(++) (original found here):
I think you misunderstood. The problem is not the backslash, it is the fact that using typeid will give you undesired functionality (namely, instead of a compile-time string substitution for an object’s type, you will get an instance of the C++ class std::type_info.
My suggestion was the following: if this kind of feature is really that important to you, you should use standard ISO C++, which includes support for iterators and templates. You can use std::for_each or try out some of the more programmer-friendly options from boost. Rewrite your program to use this portable functionality.
If, however, you want to be hackish, you can create a header like the one linked above. Then, change typeid back to typeof (or to whatever identifier you chose for the macro in your header file) so that it all works out.
P.S.
It is unnecessary to repost unchanged code samples.
P.P.S
When posting future code, please enclose them with between [code] and [/code].
GCC is a compiler collection, and you can choose which standard to use when compiling with the "-std=" option.
2°)
Quote:
I prefer not to convert them to normal functions but keep it the way if it could be working under windows by some tricks.
Why? Kernel code is the kind of code where heavy use of macros could be interesting... But for "normal" programming it is not a good practice since it is really dangerous for many reasons. You'd better use function and if performance really matter you could forcing them as inlined.
3°)
Quote:
looks like the '\' at the end of each lines is not compatible in VC, but the site you refer to is using this, weird, right ?
backslash is just an escape character, so the precompiler won't take in account the "newline" at the end of the line, which is necessary there since macro must stand in one line only.
By the way, if you're programming in C++, use containers like those in the STL, boost or any wide-spread portable libraries, this is true for C too, but I'm not aware of any containers library widely used, maybe glib from gtk+?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.