LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Thread Tools
Old 06-27-2009, 02:45 PM   #1
Kunsheng
Member
 
Registered: Mar 2009
Posts: 82
Thanked: 1
Way to convert Macro Defined functions from linux Kernel in Visual C++


[Log in to get rid of this advertisement]
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))

-------------------------------------------------------------------------

And in user space code, I call it in this way:

--------------------------------------------------------

list_for_each_entry_safe( pcre_item, tmp,
&(((pcre_list_head_t *)(hr.value))->head), list)
{
// My code to handle each element in the list
}

----------------------------------------------------------


This is working like a charm in Linux, but I got errors in Windows:

It reminds me missing ';' after 'list_for_each_entry_safe( pcre_item, tmp,
&(((pcre_list_head_t *)(hr.value))->head), list)'.


Does this kind of linux code not working in Windows at all? (Linux is in GCC C stand and VC is ANSI C)

I prefer not to convert them to normal functions but keep it the way if it could be working under windows by some tricks.


Any idea?


Thanks,

-Kun

Last edited by Kunsheng; 06-27-2009 at 02:49 PM..
Kunsheng is offline     Reply With Quote
Old 06-28-2009, 02:58 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 1,034
Thanked: 50
Does your code under Windows include a file where the needed macros are defined ?

Or do you the needed macro definitions copy-pasted into your code ?

Last edited by Sergei Steshenko; 06-28-2009 at 03:22 PM..
Sergei Steshenko is offline     Reply With Quote
Old 06-28-2009, 04:24 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,434
Thanked: 24
Quote:
Originally Posted by Kunsheng View Post
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 10:20 PM.. Reason: clarification
osor is offline     Reply With Quote
Old 06-28-2009, 08:44 PM   #4
Kunsheng
Member
 
Registered: Mar 2009
Posts: 82
Thanked: 1

Original Poster
Thanks, osor. I checked the site you refer to and it provides some #define functions similar to ones I got problem.

The biggest problem is that the following #define macros

-------------------------------------------------------------------------

#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))

-------------------------------------------------------------------------

is not working in my user space program in this way:

--------------------------------------------------------

list_for_each_entry_safe( pcre_item, tmp,
&(((pcre_list_head_t *)(hr.value))->head), list)
{
// My code to handle each element in the list
}

----------------------------------------------------------


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 View Post
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):
Code:
#define typeof(expression) CtypeRegRoot<var_type_id(expression)>::id2type::Type
Kunsheng is offline     Reply With Quote
Old 06-28-2009, 10:20 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,434
Thanked: 24
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].
osor is offline     Reply With Quote
Old 06-29-2009, 10:25 AM   #6
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 308
Thanked: 14
Some remarks:

1°)
Quote:
(Linux is in GCC C stand and VC is ANSI C)
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+?
jf.argentino is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
running Visual Basic/Visual Fox Pro in Linux? depam Linux - Software 9 02-11-2006 04:42 PM
how to use c functions in linux kernel mode pwangee Linux - General 5 07-12-2004 11:21 PM
how to use c functions in linux kernel mode pwangee Linux - Software 2 07-12-2004 12:23 AM
FYI: Creating User Defined Functions for MySQL mchirico LinuxQuestions.org Member Success Stories 1 06-11-2004 09:51 PM


All times are GMT -5. The time now is 10:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration