C++ or pure C for Linux kernel module, Linux device driver development. What to use?
ProgrammingThis 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.
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.
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
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.
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++.
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++.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.