clinuxlinux-kernelkernelprintk

undefined reference to `printk'


I want to use printk function in my userspace code, but I don't want to write kernel module. Is it any possibility to do that?

I tried use linux/kernel.h header and linux/module.h but it doesn't work

printk("<1>some text");

Solution

  • Simple Answer is No, You can't use printk in userspace code by any means. printk is designed for kernel programmers. If your intention is to write to syslog -> dmesg, then use syslog() ; It comes in handy!!

    Syslog ManPage

    Try This:

    #include <stdio.h>
    #include <unistd.h>
    #include <syslog.h>
    
    int main(void) {
    
     openlog("slog", LOG_PID|LOG_CONS, LOG_USER);
     syslog(LOG_EMERG, "Hello from my code ");
     closelog();
    
     return 0;
    }
    

    To Configure syslog for file redirection:

    http://www.softpanorama.org/Logs/syslog.shtml

    http://linux.die.net/man/5/syslog.conf