On Sat, 15 Apr 2000, Brian Dunstan wrote:
> I've been trying to get Vector Library, which is written in C++, to work
> in an rtlinux module which has to multiply and solve matrices analyzing
> data in real-time. When I try to switch to the g++ compiler, a whole
> host of errors appears are reported from the rtlinux sources. Running
> g++ with the '-traditional' flag produced no change in the rusults...
> does anyone have any insights or experience on getting g++ to compile
> rtlinux code?
Hi,
I also encountered various problems when I attempeted to use g++
to compile rtlinux code. Here are problems and my workarounds:
1. Header file problems:
Linux kernel header files are not prepared for g++.
Standard header files (e.g. stdio.h) can be included by
C++ source code because they are surrounded by following code:
#ifdef __cplusplus
extern "C" {
#endif
.... // header file contents
#ifdef __cplusplus
}
#endif
You have to do it explicitly in C++ source file:
extern "C" {
#include <some-kernel-header-file>
}
Furthermore, some kernel data structure uses C++ keywords such as
'new' as element names. My solution was:
#define new _new
#include <...>
#undef new
There is difference how NULL is defined for C and C++. In C,
NULL is usually (void*)0, but in C++ NULL should be 0 to avoid
many warning messages. Following is part of my source code.
extern "C" {
#define new _new
#define NULL 0
#include <linux/kernel.h>
#include <linux/module.h>
#include <rtl_sched.h>
#include <rtl_time.h>
#undef new
}
2. Compile options
You cannot use exceptions nor RTTI. Use -fno-exceptions -fno-rtti
compile options.
3. new and delete
Generally, dynamic allocation should be avoided, but sometimes it
is necessary in C++ programs. I used following functions:
void * operator new (size_t size)
{
return kmalloc(size, GFP_KERNEL);
}
void * operator new[] (size_t size)
{
return kmalloc(size, GFP_KERNEL);
}
// placement new -- see item 4
void * operator new (size_t size, void *ptr)
{
return ptr;
}
void operator delete(void *ptr)
{
kfree(ptr);
}
void operator delete[](void *ptr)
{
kfree(ptr);
}
4. Global objects
I didn't tried to port C++ startup code (which is written in assembly)
to kernel environment. So if you use some global objects, their
constructors will not be called. You can either dynamically allocate
the object (which slows down access to the object and is prone to
memory leak), or call the constructor explicitly in module init
function using C++'s "placement new" syntax as follows:
class A {...};
A a; // constructor not called automatically if you don't link
// C++ startup code
int init_module()
{
....
new (&a) A(...); // calls constructor with argument ...
// the 'placement-new' function in item 3
// should be defined somewhere
....
}
void cleanup_module()
{
a.~A(); // calls destructor
}
Hope this helps.
--
Yunho Jeon +82-2-875-9183
Ph.D. Student, Elec. Engr. http://csl.snu.ac.kr/~yunho
Seoul Nat'l Univ., Korea e-mail: yunho at csl.snu.ac.kr
-- [rtl] ---
To unsubscribe:
echo "unsubscribe rtl" | mail [EMAIL PROTECTED] OR
echo "unsubscribe rtl <Your_email>" | mail [EMAIL PROTECTED]
---
For more information on Real-Time Linux see:
http://www.rtlinux.org/rtlinux/