Domanda

I am editing a kernel module written in C++. I want a function to print a message but i guess i can not use cout or any Standard Input/Output Streams library function because the module resides in kernel space.

I have tried to include the iostream library:

....
#include <iostream>
....

When i compile with g++ i get:

 error: iostream: No such file or directory
 error: 'cout' was not declared in this scope
È stato utile?

Soluzione 2

The Linux kernel does not support C++ very well at all, and you can't use the <iostream> - I suspect any of the STL is also completely out of the question, but it's been quite some time since I looked at C++ in Linux kernel mode.

The standard implementation of cout and cerr are using OS features, which of course won't work inside the kernel. So you mustn't use those. It would of course be possible to implement something that works the same way (call it kostream), and kostream& operator<<(kostream& os, sometype value), which produces some output into a buffer, then use printk or some of the primitives that printk uses to produce the actual output.

No matter whether it is C or C++, you will eventually end up using printk (or it's constituent parts) to print things in the Linux kernel.

Note that using fstream or stringstream is also not going to work inside the kernel.

Altri suggerimenti

Use printk api for printing message

printk(KERN_DEBUG "*** This is a debug message only. ***\n");

You can check print messages of your kernel module to dmesg

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top