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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
08-16-2005, 02:32 PM
|
#1
|
Member
Registered: Aug 2004
Posts: 76
Rep:
|
C++ or pure C for Linux kernel module, Linux device driver development. What to use?
C++ or pure C for Linux kernel module, Linux device driver development. What to use? What is mostly used?
In Internet I met many discussions. Many arguments for C and C++
Should we blindly follow what Linus said?
Is Linux Kernel supposed to be ported to C++?
What is mostly used C++ or C for kernel module development in open source and commercial development
What do you know and herad and think about that question?
What du you know about this C++ support http://netlab.ru.is/exception/LinuxCXX.shtml
Thanks in advance
|
|
|
08-16-2005, 02:41 PM
|
#2
|
Member
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340
Rep:
|
Umm... what are you good at? Nobody is going to shoot you for writing a driver in C++. If you know how to code better at C++ then use it! Its very easy to link C++ code to C libraries, and I'm sure you can write inline ASM the same way you can in C. With that said I dont see any reason not to use C++, especially if its YOUR project. If others dont like the way you're doing things, tell them to write it their way and send you a patch. Most of them you wont hear from again 
|
|
|
08-17-2005, 04:15 AM
|
#3
|
Member
Registered: Aug 2004
Posts: 76
Original Poster
Rep:
|
Thanks But the kernel is compiled with gcc not g++ If I write in c++ I should compile them separately???
|
|
|
08-17-2005, 12:44 PM
|
#4
|
Member
Registered: May 2005
Distribution: Debian Woody,Knoppix
Posts: 88
Rep:
|
Well as I know, modules are simple .o (object files). So, it does matter if it's C or C++... you just need to tell gcc/g++ only to compile and not to link (man g++). Then you can insert the module in the kernel.
|
|
1 members found this post helpful.
|
08-17-2005, 01:36 PM
|
#5
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,270
|
Not so fast... does the necessary programming-environment exist in the kernel-space environment? I don't know but I don't think that it does.
A kernel module becomes part of the kernel. It has no access to "runtime libraries," which is an essential part of the high-level niceities of a language like C++.
|
|
1 members found this post helpful.
|
08-17-2005, 01:44 PM
|
#6
|
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Rep:
|
You cannot write a kernel module in C++.
At least... not easily, and it isn't guarenteed to work.
Quote:
In fact, in Linux we did try C++ once already, back in 1992. It sucks. Trust me - writing kernel code in C++ is a BLOODY STUPID IDEA.
The fact is, C++ compilers are not trustworthy. They were even worse in
1992, but some fundamental facts haven't changed: 1) the whole C++ exception handling thing is fundamentally broken. It's _especially_ broken for kernels. 2) any compiler or language that likes to hide things like memory allocations behind your back just isn't a good choice for a kernel. 3) you can write object-oriented code (useful for filesystems etc) in C, _without_ the crap that is C++.
|
-- Linus
|
|
1 members found this post helpful.
|
08-17-2005, 01:58 PM
|
#7
|
Member
Registered: Aug 2004
Posts: 76
Original Poster
Rep:
|
What makefile should be for compilation of c++ kernel module
|
|
|
08-17-2005, 02:13 PM
|
#9
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
From what I read, the GCC compiler has incorporated some new features (C99) that adopted some changes from C++. There is a book by Novel on Linux kernel development that went into details in how the GCC compiler differs from vanilla C, but I don't remember the details.
|
|
|
08-17-2005, 02:31 PM
|
#10
|
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Rep:
|
Quote:
Originally posted by jschiwal
From what I read, the GCC compiler has incorporated some new features (C99) that adopted some changes from C++. There is a book by Novel on Linux kernel development that went into details in how the GCC compiler differs from vanilla C, but I don't remember the details.
|
Technically speaking gcc IS a C++ compiler.. it is also a C compiler. The point is, there is no support in the kernel runtime for C++. The only way to get it to work at all is to install some patches that have been notoriously unstable.
|
|
|
08-20-2005, 05:33 AM
|
#11
|
Member
Registered: Aug 2004
Posts: 76
Original Poster
Rep:
|
How to make Linux programming more comfortable
C++ is more efficient compiled than C now Maybe in 1992 it was Vice versa
?????
|
|
|
08-20-2005, 04:03 PM
|
#12
|
Moderator
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696
|
Why do you think C++ compiler is more efficient? C++ adds a number of layers (read: more code) that's not needed (and wanted) at the kernel level.
|
|
|
08-20-2005, 10:05 PM
|
#13
|
LQ Guru
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507
Rep: 
|
Igor, can you demonstrate this?
My testing indicates a few things. First off, I used the defacto standards for output (printf in c, cout in c++), and attempted to create a relatively simple program.
First off, the code:
Code:
$ cat c_test.c
#include <stdio.h>
int main(int argc,char **argv){
int i;
for(i=0;i<1000;i++)
printf("%d\n",i);
return 0;
}
$ cat cpp_test.cpp
#include <iostream>
using namespace std;
int main(int argc,char **argv){
int i;
for(i=0;i<1000;i++)
cout << i << endl;
return 0;
}
I then performed a compilation, and a "generate assembly" step on both.
Code:
$ gcc -o c_test c_test.c
$ g++ -o cpp_test cpp_test.cpp
$ gcc -s c_test.c
$ g++ -s cpp_test.cpp
I then examined the lengths of each assembly file:
Code:
$ wc -l *.s
34 c_test.s
223 cpp_test.s
And byte sizes of compiled binaries:
Code:
$ wc -c *_test
7285 c_test
9250 cpp_test
Running "time ./EXECUTABLE" for c_test and cpp_test give (output removed)
Code:
$ time ./c_test
real 0m0.007s
user 0m0.001s
sys 0m0.005s
$ time ./cpp_test
real 0m0.010s
user 0m0.007s
sys 0m0.002s
Not a huge difference, but still, for such a simple program, quite notable.
I welcome any studies to refute this.
|
|
1 members found this post helpful.
|
08-21-2005, 04:15 PM
|
#14
|
Member
Registered: Aug 2004
Posts: 76
Original Poster
Rep:
|
It is out of comparison
Matir
Iostream things of c++ is usage of library the big hierarchy of classes the huge code is added when the iostream things are used So it is out of comparison.
Print functions can be used in C++ too.
I mean the whole program code including many functions and data classes.
Take emulated C class with function pointers and data and take c++ class with all these features.
Some guys says that in general now C++ code is equally and sometimes is better compiled than C code.
|
|
|
08-21-2005, 04:24 PM
|
#15
|
LQ Guru
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507
Rep: 
|
Again, prove your point please. I would leave to see such a demonstration.
|
|
|
All times are GMT -5. The time now is 08:27 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|