Find the answer to your Linux question:
Results 1 to 9 of 9
HI First of all I am a newbie to linux and compiling etc, but reasonably pc literate and trying to teach myself. I have been practicing compiling the kernel (I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2003
    Location
    UK-Heart of Cheshire
    Posts
    31

    Comiling messages and variable using gcc


    HI

    First of all I am a newbie to linux and compiling etc, but reasonably pc literate and trying to teach myself. I have been practicing compiling the kernel (I know bit extreme practice for a novice) but would like to understand some messages etc.

    1. When using gcc it looks like it is passing variables such as -D__KERNEL__ or -DMODULES. From the help I can see the -D is a command for macros. The question I have is what are macros in this case, where are the defined etc? are they just varible substitutions (if so why not use parameters)

    2. I get a message about string literal concatanation using __FUNCTION__ is deprication. I can understand that the source program is using a command the will be desupported eventually but can anyone explian this in detail. What should be used instead etc.

    3 I have a feeling that these are c and c++ issues. Anyone know a good (simple starting point) for understanding c and c++

    Thanks for any help

  2. #2
    Linux User
    Join Date
    Feb 2003
    Location
    Norway, Asker
    Posts
    267

    Re: Comiling messages and variable using gcc

    Quote Originally Posted by turls
    HI
    3 I have a feeling that these are c and c++ issues. Anyone know a good (simple starting point) for understanding c and c++

    Thanks for any help
    http://home.twcny.rr.com/amantoan/cweb/Cindex.htm

    that is a good tutorial

  3. #3
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Well, to begin with, when compiling a C program (to a relocatable, not an executable), there are two major steps involved: preprocessing and compiling.
    You see, every C program that gets compiled is first passed through the C preprocessor (or cpp from the shell, many other programs use it as well), which basically just does some macro substitution and other pretty basic things before the actual compiler handles the code. For example, it handles the #include directives in the beginning of the files, it removes comments, etc.
    The -D__KERNEL__ tells the preprocessor to define a macro called __KERNEL__, which, if used, would be substituted with nothing. But that's not its purpose. There are #ifdef directives that are handled by the preprocessor as well, so the programmer can decide which parts of the code to compile depending on whether the file is being compiled as part of the kernel or not. Some files in the kernel tree are used outside of the kernel as well. Same things with -DMODULES, the programmer can decide that some parts shouldn't be compiled if the file isn't being compiled as a module.
    The __FUNCTION__ is built-in preprocessor macro, which always expands to the current function being compiled. For some reason the GCC guys have decided to depricate string concatenation usage with this (string concatenation: if you write "Hello " "world" "!" in C, it will concatenate all the strings to "Hello world!"). My guess is that they're moving it from being a preprocessor macro to being a symbol built in to the actual compiler.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    May 2003
    Location
    UK-Heart of Cheshire
    Posts
    31
    So if my understanding is correct by using the __KERNEL__ macro (which in this case is null) the include programs check where value of this macro and then decide on an action. For example whether a module is compiled into the Kernel or is left as a module.

    If the -D__KERNEL__ id followed by -I/directory/include (not equals) is the macro just acting like a parameter replacing the __KERNAL__ with the include file?

    Thx for the help

  6. #5
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Not exactly. The __KERNEL__ is primarily used by files that are used both in the kernel and in projects (to see if they can use libc functions or must use the kernel's replacements, for example). The MODULE macro is checked for whether the file is compiled as part of the kernel or as a module.

    And no, -D__KERNEL__ followed by a -I does not do what you describe. To make the substitution value of __KERNEL__ non-empty, it would have to be declared as -D__KERNEL__=foo, for example. The -I's are seperate stuff that tells the preprocessor which directories to search for header files.

  7. #6
    Just Joined!
    Join Date
    May 2003
    Location
    UK-Heart of Cheshire
    Posts
    31
    I thought I was understanding it . Ok with the -I.

    Still confused about the __KERNEL__ (or any of the other macros).

    I understand the by using -D__KERNEL__ this declares the __KERNEL__ macro and the fact that ther is no =....... after it, it is empty. Ok there!

    What I do not understand is how this or any other macro is used is then used.

    Is it correct that these macros are not defined anywhere before being used here.

    Any example would be useful.
    PS Thx. i am getting there slowly

  8. #7
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    I thought of giving an example, but for some reason, I didn't. One good example is the file fs/intermezzo/kml_unpack.c under you kernel tree. It has a number of utility functions for the other parts of the Intermezzo file system to use. It uses __KERNEL__ a number of times, since it is used both by the kernel part of the file system, and by the user mode server. Because of that, it is sometimes linked against the standard libc, and sometimes against the kernel's own replacement library functions. For example, look at this code:
    Code:
    static char *print_time(__u64 i)
    {
    	char buf[128];
    	
    	memset(buf, 0, 128);
    
    #ifndef __KERNEL__
    	strftime(buf, 128, "%Y/%m/%d %H:%M:%S", gmtime((time_t *)&i));
    #else
    	sprintf(buf, "%Ld\n", i);
    #endif
    
    	return strdup(buf);
    }
    As you can see, if this is being compiled into a userland tool, it uses the strftime() function to format the time in a human-readable format. However, if compiled in the kernel, the strftime function doesn't exist, and it just dumps the time in the standard UNIX time format, an integer number showing the number of seconds having passed since 01 Jan 1970 00:00.
    It also uses __KERNEL__ in the beginning to decide what header files to use.

  9. #8
    Just Joined!
    Join Date
    May 2003
    Location
    UK-Heart of Cheshire
    Posts
    31
    Sussed it thxs fro the patience and help . Found a very good tutorial on http://www.cs.cf.ac.uk/Dave/C/CE.html.

    From what you have said and the tutorial. The -D commands either set values ie if =xyz is used or are just "Set" so that the macro "exists". The fact that the macro exists is then recognised in the other programs that can use this as a decision point, to alter or vary the code included.

    Really just configurable if ststement!

    Once again thx!

  10. #9
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Yeah, that's just about it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •