Thread: error: was not declared in this scope compilation error

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    1

    error: was not declared in this scope compilation error

    hi all,
    im getting the following compilation error when trying to compile a file under /proc using 5.5 RHEL
    Code:
    /*
     *  procfs1.c -  create a "file" in /proc
     *
     */
    #include <linux/module.h> /* Specifically, a module */
    #include <linux/kernel.h> /* We're doing kernel work */
    #include <linux/proc_fs.h> /* Necessary because we use the proc fs */
    #define procfs_name "helloworld"
    /**
     * This structure hold information about the /proc file
     *
     */
    struct proc_dir_entry *Our_Proc_File;
    /* Put data into the proc fs file.
     * 
     * Arguments
     * =========
     * 1. The buffer where the data is to be inserted, if
     *    you decide to use it.
     * 2. A pointer to a pointer to characters. This is
     *    useful if you don't want to use the buffer
     *    allocated by the kernel.
     * 3. The current position in the file
     * 4. The size of the buffer in the first argument.
     * 5. Write a "1" here to indicate EOF.
     * 6. A pointer to data (useful in case one common 
     *    read for multiple /proc/... entries)
     *
     * Usage and Return Value
     * ======================
     * A return value of zero means you have no further
     * information at this time (end of file). A negative
     * return value is an error condition.
     *
     * For More Information
     * ====================
     * The way I discovered what to do with this function
     * wasn't by reading documentation, but by reading the
     * code which used it. I just looked to see what uses
     * the get_info field of proc_dir_entry struct (I used a
     * combination of find and grep, if you're interested),
     * and I saw that  it is used in <kernel source
     * directory>/fs/proc/array.c.
     *
     * If something is unknown about the kernel, this is
     * usually the way to go. In Linux we have the great
     * advantage of having the kernel source code for
     * free - use it.
     */
    int
    procfile_read(char *buffer,
           char **buffer_location,
           off_t offset, int buffer_length, int *eof, void *data)
    {
     int ret;
     
     printk(KERN_INFO "procfile_read (/proc/%s) called\n", procfs_name);
     
     /* 
      * We give all of our information in one go, so if the
      * user asks us if we have more information the
      * answer should always be no.
      *
      * This is important because the standard read
      * function from the library would continue to issue
      * the read system call until the kernel replies
      * that it has no more information, or until its
      * buffer is filled.
      */
     if (offset > 0) {
      /* we have finished to read, return 0 */
      ret  = 0;
     } else {
      /* fill the buffer, return the buffer size */
      ret = sprintf(buffer, "HelloWorld!\n");
     }
     return ret;
    }
    int init_module()
    {
     Our_Proc_File = create_proc_entry(procfs_name, 0644, NULL);
     
     if (Our_Proc_File == NULL) {
      remove_proc_entry(procfs_name, &proc_root);
      printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
             procfs_name);
      return -ENOMEM;
     }
     Our_Proc_File->read_proc = procfile_read;
     Our_Proc_File->owner   = THIS_MODULE;
     Our_Proc_File->mode   = S_IFREG | S_IRUGO;
     Our_Proc_File->uid   = 0;
     Our_Proc_File->gid   = 0;
     Our_Proc_File->size   = 37;
     printk(KERN_INFO "/proc/%s created\n", procfs_name); 
     return 0; /* everything is ok */
    }
    void cleanup_module()
    {
     remove_proc_entry(procfs_name, &proc_root);
     printk(KERN_INFO "/proc/%s removed\n", procfs_name);
    }
    here are the compilcation errors im getting:
    Code:
    [root@SRV-00-035 home]# g++ procfs1.cpp -o procfs1 -v -I /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/
    Using built-in specs.
    Target: x86_64-redhat-linux
    Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release 
    --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile 
    --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre 
    --with-cpu=generic --host=x86_64-redhat-linux
    Thread model: posix
    gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)
     /usr/libexec/gcc/x86_64-redhat-linux/4.1.2/cc1plus -quiet -v -I /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/ -D_GNU_SOURCE procfs1.cpp -quiet 
    -dumpbase procfs1.cpp -mtune=generic -auxbase procfs1 -version -o /tmp/ccD2mF0Q.s
    ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../x86_64-redhat-linux/include"
    #include "..." search starts here:
    #include <...> search starts here:
     /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/
     /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2
     /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/x86_64-redhat-linux
     /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/backward
     /usr/local/include
     /usr/lib/gcc/x86_64-redhat-linux/4.1.2/include
     /usr/include
    End of search list.
    GNU C++ version 4.1.2 20080704 (Red Hat 4.1.2-48) (x86_64-redhat-linux)
            compiled by GNU C version 4.1.2 20080704 (Red Hat 4.1.2-48).
    GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
    Compiler executable checksum: ad35c03ce9997656f54e642ebd49f167
    In file included from /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/lockdep.h:13,
                     from /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/spinlock_types.h:18,
                     from /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/spinlock.h:78,
                     from /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/module.h:10,
                     from procfs1.cpp:6:
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/list.h:1031:2: warning: #warning "don't include kernel headers in userspace"
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/bitops.h: In function גint get_bitmask_order(unsigned int)ג:
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/bitops.h:15: error: גflsג was not declared in this scope
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/bitops.h: In function גint get_count_order(unsigned int)ג:
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/bitops.h:23: error: גflsג was not declared in this scope
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/bitops.h: In function גlong unsigned int hweight_long(long unsigned int)ג:
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/bitops.h:31: error: גhweight32ג was not declared in this scope
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/bitops.h:31: error: גhweight64ג was not declared in this scope
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/bitops.h: In function גunsigned int fls_long(long unsigned int)ג:
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/bitops.h:99: error: גflsג was not declared in this scope
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/bitops.h:100: error: גfls64ג was not declared in this scope
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/bitops.h: At global scope:
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/bitops.h:103: error: ג__ffs64ג declared as an גinlineג variable
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/bitops.h:103: error: גu64ג was not declared in this scope
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/bitops.h:104: error: expected ג,ג or ג;ג before ג{ג token
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/asm/system.h:180: error: expected ג,ג or ג...ג before גnewג
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/asm/system.h: In function גlong unsigned int __cmpxchg(volatile void*, long unsigned int, long unsigned int)ג:
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/asm/system.h:183: error: גsizeג was not declared in this scope
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/asm/system.h:187: error: expected type-specifier before ג)ג token
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/asm/system.h:193: error: expected type-specifier before ג)ג token
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/asm/system.h:199: error: expected type-specifier before ג)ג token
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/asm/system.h:205: error: expected type-specifier before ג)ג token
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/spinlock.h: In function גvoid smp_mb__after_lock()ג:
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/spinlock.h:257: error: גbarrierג was not declared in this scope
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/marker.h: At global scope:
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/marker.h:33: error: גva_listג has not been declared
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/percpu.h: In function גvoid* __alloc_percpu(size_t)ג:
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/percpu.h:51: error: גGFP_KERNELג was not declared in this scope
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/percpu.h:51: error: גkmallocג was not declared in this scope
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/percpu.h:53: error: גmemsetג was not declared in this scope
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/percpu.h: In function גvoid free_percpu(const void*)ג:
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/percpu.h:58: error: גkfreeג was not declared in this scope
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/module.h: At global scope:
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/module.h:51: error: field גattrג has incomplete type
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/module.h:62: error: field גkobjג has incomplete type
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/proc_fs.h:79: error: field גlistג has incomplete type
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/proc_fs.h:249: error: expected ג,ג or ג...ג before גnewג
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/proc_fs.h:264: error: field גvfs_inodeג has incomplete type
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/proc_fs.h: In function גproc_inode* PROC_I(const inode*)ג:
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/proc_fs.h:269: error: expected primary-expression before גstructג
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/proc_fs.h:269: error: גvfs_inodeג was not declared in this scope
    /usr/src/kernels/2.6.18-194.el5-xen-x86_64/include/linux/proc_fs.h:269: error: גcontainer_ofג was not declared in this scope
    procfs1.cpp: In function גint procfile_read(char*, char**, off_t, int, int*, void*)ג:
    procfs1.cpp:61: error: גKERN_INFOג was not declared in this scope
    procfs1.cpp:61: error: גprintkג was not declared in this scope
    procfs1.cpp:79: error: גsprintfג was not declared in this scope
    procfs1.cpp: In function גint init_module()ג:
    procfs1.cpp:91: error: גKERN_ALERTג was not declared in this scope
    procfs1.cpp:92: error: גprintkג was not declared in this scope
    procfs1.cpp:98: error: גS_IRUGOג was not declared in this scope
    procfs1.cpp:103: error: גKERN_INFOג was not declared in this scope
    procfs1.cpp:103: error: גprintkג was not declared in this scope
    procfs1.cpp: In function גvoid cleanup_module()ג:
    procfs1.cpp:110: error: גKERN_INFOג was not declared in this scope
    procfs1.cpp:110: error: גprintkג was not declared in this scope
    [root@SRV-00-035 home]#
    Last edited by Salem; 05-27-2010 at 08:42 AM. Reason: tags fixed

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Obviously you now how to use code tags, why not put them around your -code-?

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    warning "don't include kernel headers in userspace
    I've never tried to do this, so I don't know how significant the warning is -- based on what I do know of kernel programming, it may be impossible. Have you ever done this successfully? I've written some modules (I believing adding to /proc counts as 100% a kernelspace affair and should be done with a kernel module) but there is a specific technique for compiling them, and this is not it.

    If you have not done this successfully before, give up this approach now: it looks like maybe you do need to start reading some documentation. Google "writing linux device drivers" or "linux modules". Of course, you are not creating a device driver for a real device, but it is the same thing: remember that in unix, all devices are files. Hence, you can have files that are devices that do not have a hardware correlate (if devices are files, files are in a sense devices). Proc is the same. In fact, there is a section on proc in this book, which is free online:

    http://lwn.net/Kernel/LDD3/

    starts on page 83, but you will probably have to read the stuff leading up to this to understand the context properly.
    Last edited by MK27; 05-27-2010 at 07:53 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  3. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM