Cpp in kernelspace

Martin Hansen mah_list1 at cfsi.dk
Tue Jun 7 16:54:36 CEST 2005


On Fri, 2005-05-27 at 13:31 +0200, Peter Soetens wrote:
> I believe the aim is that you manually link with the crtbegin.o and crtend.o 
> files with your kernel module. RTAI actually provides three options :

I solved the problem so far. I made a small test program
But to make the global constructors run i needed a to link manualy with
crtbegin in the gcc lib giving these command lines:

gcc -c -o rtai_ramp.o rtai_ramp.c -O2  -DMODULE  -D__KERNEL__ -Wall
-I/lib/modules/`uname -r`/build/include -I/usr/realtime/include -I.

g++ -c -o function_lib.o function_lib.cpp -O2  -DMODULE  -D__KERNEL__
-Wall

ld -m elf_i386  -r  -o
ramp_demo.o  /usr/lib/gcc-lib/i486-linux/3.3.5/crtbegin.o /usr/src/rtai/rtai-addons/cpp/crtsbegin.o rtai_ramp.o  function_lib.o  /usr/lib/gcc-lib/i486-linux/3.3.5/crtend.o /usr/src/rtai/rtai-addons/cpp/crtsend.o

Notice that i link with both the crtbegin from gcc and with crtsbegin
from rtai. Can it be true that i have to do that?
It si also important that i give the files in this order to the linker,
or the global contructor wont be called.

It would be nice to be able to tell gcc to take the crtbegin/end libs by
itself as the path can change depending on compiler instalation.

my small test consist of:

function_lib.cpp
-----------------------------------------
#include <linux/types.h>
#include "function_lib.h"

class testclass{
public:

 testclass(int ain, int bin){
    a=ain;
    b=bin;
  }
  int  get_a()
  {
    return a;
  }

  int  get_b()
  {
    return b;
  }

private:
  int a, b;

};



testclass testobject(10, 20);




int c_get_a(){
  return testobject.get_a();
}

int c_get_b(){
  return testobject.get_b();
}
-------------------------------------------------------------
rtai_ramp.c
-------------------------------------------------------------
#include <rtai.h>
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

#include "function_lib.h"

#include "rtai_cpp.h"

extern void init_cpp( void );


int __init xinit_module(void)
{
  init_cpp();
  rt_printk(" a= %i b= %i \n", c_get_a(), c_get_b());
  return 0;
}

void xcleanup_module(void)
{
  cleanup_cpp();
}

module_init(xinit_module);
module_exit(xcleanup_module);
MODULE_LICENSE("GPL");

-------------------------------------------------------------------------------
function_lib.h
-------------------------------------------------------------------------------
#ifndef _FUNCTION_LIB_H
#define _FUNCTION_LIB_H

#ifdef __cplusplus
extern "C" {
#endif

  int c_get_a(void );
  int c_get_b(void);

#ifdef __cplusplus
}
#endif

#endif /*_FUNCTION_LIB_H */





More information about the Rtai mailing list