LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 08-16-2005, 02:32 PM   #1
Igor007
Member
 
Registered: Aug 2004
Posts: 76

Rep: Reputation: 15
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
 
Old 08-16-2005, 02:41 PM   #2
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
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
 
Old 08-17-2005, 04:15 AM   #3
Igor007
Member
 
Registered: Aug 2004
Posts: 76

Original Poster
Rep: Reputation: 15
Thanks But the kernel is compiled with gcc not g++ If I write in c++ I should compile them separately???
 
Old 08-17-2005, 12:44 PM   #4
NCC-1701&NCC-1701-D
Member
 
Registered: May 2005
Distribution: Debian Woody,Knoppix
Posts: 88

Rep: Reputation: 16
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.
Old 08-17-2005, 01:36 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.
Old 08-17-2005, 01:44 PM   #6
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
Old 08-17-2005, 01:58 PM   #7
Igor007
Member
 
Registered: Aug 2004
Posts: 76

Original Poster
Rep: Reputation: 15
What makefile should be for compilation of c++ kernel module
 
Old 08-17-2005, 02:02 PM   #8
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Read the LKML thread here: http://kerneltrap.org/node/2067

As the authors of http://netlab.ru.is/exception/LinuxCXX.shtml point out, their support ONLY works for i386 architecture. Unless your driver is i386 specific, then forget it.
 
Old 08-17-2005, 02:13 PM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 08-17-2005, 02:31 PM   #10
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
Old 08-20-2005, 05:33 AM   #11
Igor007
Member
 
Registered: Aug 2004
Posts: 76

Original Poster
Rep: Reputation: 15
How to make Linux programming more comfortable

C++ is more efficient compiled than C now Maybe in 1992 it was Vice versa
?????
 
Old 08-20-2005, 04:03 PM   #12
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 08-20-2005, 10:05 PM   #13
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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.
Old 08-21-2005, 04:15 PM   #14
Igor007
Member
 
Registered: Aug 2004
Posts: 76

Original Poster
Rep: Reputation: 15
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.
 
Old 08-21-2005, 04:24 PM   #15
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Again, prove your point please. I would leave to see such a demonstration.
 
  


Reply

Tags
arm, c++, compile, fine, kernel, linux, modules



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
Linux kernel development vwal_13 Programming 3 03-09-2005 02:27 AM
Linux Device Driver Development aslv Linux - Certification 0 09-27-2004 08:42 AM
Linux Kernel Development impact on Slackware carboncopy Slackware 5 07-28-2004 03:43 PM
Touch screen driver development for Linux peso Linux - Software 1 02-02-2004 12:15 PM
Lexmark Released A Linux Driver Development Kit FearPasion710 Linux - Software 0 09-09-2003 06:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:51 PM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
Open Source Consulting | Domain Registration