LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ or pure C for Linux kernel module, Linux device driver development. What to use? (https://www.linuxquestions.org/questions/programming-9/c-or-pure-c-for-linux-kernel-module-linux-device-driver-development-what-to-use-353924/)

Igor007 08-16-2005 02:32 PM

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

lowpro2k3 08-16-2005 02:41 PM

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 ;)

Igor007 08-17-2005 04:15 AM

Thanks But the kernel is compiled with gcc not g++ If I write in c++ I should compile them separately???

NCC-1701&NCC-1701-D 08-17-2005 12:44 PM

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.

sundialsvcs 08-17-2005 01:36 PM

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++.

jtshaw 08-17-2005 01:44 PM

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

Igor007 08-17-2005 01:58 PM

What makefile should be for compilation of c++ kernel module

Matir 08-17-2005 02:02 PM

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.

jschiwal 08-17-2005 02:13 PM

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.

jtshaw 08-17-2005 02:31 PM

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.

Igor007 08-20-2005 05:33 AM

How to make Linux programming more comfortable
 
C++ is more efficient compiled than C now Maybe in 1992 it was Vice versa
?????

Mara 08-20-2005 04:03 PM

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.

Matir 08-20-2005 10:05 PM

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.

Igor007 08-21-2005 04:15 PM

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.

Matir 08-21-2005 04:24 PM

Again, prove your point please. I would leave to see such a demonstration.


All times are GMT -5. The time now is 06:35 AM.