Skip to contentSkip to conversation

C++ in linux kernel

LINUX-KERNEL rohit h 7 participants 10 messages Feb 8, 2008 – Feb 11, 2008

lore
Share & tools
lore

rohit h

author
#0 lore raw

Hi,
I am a kernel newbie.
I tried to insmod a C++ module containing classes, inheritance. I am getting 'unresolved symbol' error when I use the 'new' keyword. What could the problem be?

What kind of runtime support is needed ( arm linux kernel)? Is a
patch available for it?

Thanks,
Rohit

#1 lore raw

Hi

Quoted 4 lines

I am a kernel newbie.
I tried to insmod a C++ module containing classes, inheritance. I am getting 'unresolved symbol' error when I use the 'new' keyword. What could the problem be?

under using gcc, new operator use malloc by default. but linux doesn't have malloc.

Could you create custom allocater?

Quoted 2 lines

What kind of runtime support is needed ( arm linux kernel)? Is a
patch available for it?

may be nothing.

- kosaki

#2 lore raw
In reply to rohit h
Quoted 5 lines · rohit h

Hi,
I am a kernel newbie.
I tried to insmod a C++ module containing classes, inheritance. I am getting 'unresolved symbol' error when I use the 'new' keyword. What could the problem be?

That you used C++ is the problem. Use plain C and the gcc.

Quoted 2 lines

What kind of runtime support is needed ( arm linux kernel)? Is a
patch available for it?

Google for "linux kernel c++" and read on - this has been discussed several times on this list.

Bernd

Signature · 3 lines
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services
#3 lore raw
In reply to rohit h
Quoted 5 lines · rohit h

Hi,
I am a kernel newbie.
I tried to insmod a C++ module containing classes, inheritance. I am getting 'unresolved symbol' error when I use the 'new' keyword. What could the problem be?

If you want to use C++ in the kernel you would need to provide all the run time library support for it youself. It's possible, but not easy and encouraged

-Andi

#4 lore raw
In reply to rohit h

2008/2/8, rohit h <[email protected]>:

Quoted 9 lines

Hi,
I am a kernel newbie.
I tried to insmod a C++ module containing classes, inheritance. I am getting 'unresolved symbol' error when I use the 'new' keyword. What could the problem be?

What kind of runtime support is needed ( arm linux kernel)? Is a
patch available for it?

Quoted 8 lines

Thanks,
Rohit
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Rohit,
Please take a look at click modular router which is using c++ as a linux kernel module.
http://www.read.cs.ucla.edu/click/
The lib/glue.cc provides custom operator new.

Thanks,
Joonwoo

#5 lore raw
Quoted 7 lines · Joonwoo Park

2008/2/8, rohit h <[email protected]>:

Quoted 6 lines

Hi,
I am a kernel newbie.
I tried to insmod a C++ module containing classes, inheritance. I am getting 'unresolved symbol' error when I use the 'new' keyword. What could the problem be?

Quoted 7 lines
Quoted 3 lines

What kind of runtime support is needed ( arm linux kernel)? Is a
patch available for it?

Please take a look at click modular router which is using c++ as a linux kernel module.
http://www.read.cs.ucla.edu/click/
The lib/glue.cc provides custom operator new.

Uh, let's not make the world worse :)
Just call malloc from C++, and carefully select what C++ features you are going to use. The VMware source for example does it right.

#6 lore raw

2008/2/9, Jan Engelhardt <[email protected]>:

Quoted 12 lines · Joonwoo Park
Quoted 10 lines

2008/2/8, rohit h <[email protected]>:

Quoted 9 lines

Hi,
I am a kernel newbie.
I tried to insmod a C++ module containing classes, inheritance. I am getting 'unresolved symbol' error when I use the 'new' keyword. What could the problem be?

What kind of runtime support is needed ( arm linux kernel)? Is a
patch available for it?

Quoted 9 lines
Quoted 4 lines

Please take a look at click modular router which is using c++ as a linux kernel module.
http://www.read.cs.ucla.edu/click/
The lib/glue.cc provides custom operator new.

Uh, let's not make the world worse :)
Just call malloc from C++, and carefully select what C++ features you are going to use. The VMware source for example does it right.

Yep, also I think that if kmalloc is possible it would better than the operator new.

Rohit,
Compiling the kernel module with g++ is not a simple work, you may need big patch for kernel itself.
The c++ has more internal keywords than c.
e.g) new, delete, and ::
The new and delete should be re-spelled with another names.
The '::' is used in inline assembly however in c++ it means global namespace.

In conclusion, I don't recommand c++ for linux kernel without very special goal like the click project :)

Joonwoo

author
#7 lore raw
In reply to Jan Engelhardt
Quoted 17 lines · Jan Engelhardt
Quoted 7 lines · Joonwoo Park

2008/2/8, rohit h <[email protected]>:

Quoted 6 lines

Hi,
I am a kernel newbie.
I tried to insmod a C++ module containing classes, inheritance. I am getting 'unresolved symbol' error when I use the 'new' keyword. What could the problem be?

Quoted 7 lines
Quoted 3 lines

What kind of runtime support is needed ( arm linux kernel)? Is a
patch available for it?

Please take a look at click modular router which is using c++ as a linux kernel module.
http://www.read.cs.ucla.edu/click/
The lib/glue.cc provides custom operator new.

Quoted 6 lines

Uh, let's not make the world worse :)
Just call malloc from C++, and carefully select what C++ features you are going to use. The VMware source for example does it right.

From the links given, I dig that following C++ features need runtime support:

a. Pure virtual functions
b. Global objects : Needs compiler dependent changes c. new & delete : Can use kmalloc / kfree instead d. Run time type info
e. Exceptions
I guess features d & e need considerable effort. Apart from these, am I free to use other C++ features? Where could I see the VMWare sources?

Quoted 2 lines

Compiling the kernel module with g++ is not a simple work, you may need big patch for kernel itself.

I don't want to compile entire kernel.
I only want to compile my module with g++ and insmod it. Any hint on how to write the Makefile.

Thanks in advance,
Rohit

#8 lore raw

http://www.vmware.com/download/server/open_source.html

Quoted 54 lines · rohit h
Quoted 12 lines · Jan Engelhardt
Quoted 10 lines · Joonwoo Park

2008/2/8, rohit h <[email protected]>:

Quoted 9 lines

Hi,
I am a kernel newbie.
I tried to insmod a C++ module containing classes, inheritance. I am getting 'unresolved symbol' error when I use the 'new' keyword. What could the problem be?

What kind of runtime support is needed ( arm linux kernel)? Is a
patch available for it?

Quoted 9 lines
Quoted 4 lines

Please take a look at click modular router which is using c++ as a linux kernel module.
http://www.read.cs.ucla.edu/click/
The lib/glue.cc provides custom operator new.

Uh, let's not make the world worse :)
Just call malloc from C++, and carefully select what C++ features you are going to use. The VMware source for example does it right.

From the links given, I dig that following C++ features need runtime support:
a. Pure virtual functions
b. Global objects : Needs compiler dependent changes
c. new & delete : Can use kmalloc / kfree instead
d. Run time type info
e. Exceptions
I guess features d & e need considerable effort.
Apart from these, am I free to use other C++ features?
Where could I see the VMWare sources?

Quoted 2 lines

Compiling the kernel module with g++ is not a simple work, you may need big patch for kernel itself.

I don't want to compile entire kernel.
I only want to compile my module with g++ and insmod it. Any hint on how to write the Makefile.

Thanks in advance,
Rohit

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Signature · 2 lines
----------------------------------------------
"Why is it that every time I'm with you, makes me believe in magic?"
#9 lore raw
In reply to rohit h
Quoted 1 line · rohit h

On Feb 8, 2008 9:24 PM, Jan Engelhardt <[email protected]> wrote:

[...]

Quoted 6 lines
Quoted 2 lines

Compiling the kernel module with g++ is not a simple work, you may need big patch for kernel itself.

I don't want to compile entire kernel.
I only want to compile my module with g++ and insmod it. Any hint on how to write the Makefile.

You really should learn on the differences at run-time between between
- pure C in a hosted environment (read: you have a full libc),
- pure C in a standalone environment (read: e.g. the Linux kernel), and
- C++ in both variants.

Bernd

Signature · 3 lines
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services