GCC Header File Library Flag Listing?

This is a discussion on GCC Header File Library Flag Listing? within the Linux Programming forums, part of the Platform Specific Boards category; I've noticed that gcc requires one to specify flags for .h library files ... such a pain. Can anyone tell ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    4

    Question GCC Header File Library Flag Listing?

    I've noticed that gcc requires one to specify flags for .h library files ... such a pain. Can anyone tell me where I might find an exhaustive listing of all these flags? If it's somewhere in standard documentation, it must be very poorly marked, because I can't seem to find it!

    EDIT: Yes, it's painfully obvious that I'm a noob to programming in C ... Anyone know of some good tutorial sites?
    Last edited by LinuxSmith; 08-15-2010 at 04:23 PM. Reason: More info...

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    What do you mean, gcc requires flags to be specified for .h files? Certainly you can give gcc flags to affect its behaviour, but flags aren't required. Are you perhaps trying to compile a header file? (Because you don't compile header files, you only compile .c files into .o files, and then link the .o files together to form an executable.)

    If you have all your code in one directory, you should be able to compile it with something as simple as
    Code:
    gcc *.c -o executable
    unless you're doing something complicated, which you shouldn't be if you're just learning C.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    I wonder if he is referring to things like having to use the -lm switch for math.h, and such.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    4
    Quote Originally Posted by dwks View Post
    What do you mean, gcc requires flags to be specified for .h files? Certainly you can give gcc flags to affect its behaviour, but flags aren't required. Are you perhaps trying to compile a header file? (Because you don't compile header files, you only compile .c files into .o files, and then link the .o files together to form an executable.)

    If you have all your code in one directory, you should be able to compile it with something as simple as
    Code:
    gcc *.c -o executable
    unless you're doing something complicated, which you shouldn't be if you're just learning C.
    It didn't work like that at with the <math.h> library functions ... I had to use a -lm parameter flag to make it one of my first programs compile at all.

    ...Is there perhaps a way to avoid this tediousness, or again, where can I find such a flag listing?

    EDIT: That is *exactly* what I'm referring to kermit!
    Last edited by LinuxSmith; 08-15-2010 at 04:39 PM. Reason: More info

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    -lm is NOT a "parameter flag". It says search the library paths (specified with -L) for a library file with the name libm.a and link with it. If you wanted to link with a library called libfrobdignazzle.a, you would use -lfrobdignazzle.

    gcc(1): GNU project C/C++ compiler - Linux man page

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    4
    Quote Originally Posted by rags_to_riches View Post
    -lm is NOT a "parameter flag". It says search the library paths (specified with -L) for a library file with the name libm.a and link with it. If you wanted to link with a library called libfrobdignazzle.a, you would use -lfrobdignazzle.

    gcc(1): GNU project C/C++ compiler - Linux man page
    So where might I find a listing of all common pre-defined library names then?

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    How about the man pages? For example, man atan. Notice how it says "Link with -lm"?

    Bottom line is, the libraries used will vary from project to project. For example, I never use the math libraries, but I often use sockets (libsocket) or openssl (libssl and libcrypto). The individual libraries should tell you to with what you need to link.

    EDIT: Also in an attempt to further straighten you out on the compilation process: .h files are NOT library files; they are header files. Header files DEFINE the function (create a prototype) for the compiler, so the compiler knows if you're calling them correctly. The functions themselves are in the library file (.a file) which are brought into the program by the linker in the final step of the process.
    Last edited by rags_to_riches; 08-15-2010 at 05:26 PM.

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by rags_to_riches View Post
    -lm is NOT a "parameter flag". It says search the library paths (specified with -L) for a library file with the name libm.a and link with it. If you wanted to link with a library called libfrobdignazzle.a, you would use -lfrobdignazzle.

    gcc(1): GNU project C/C++ compiler - Linux man page
    This does not have so much to do with gcc as it does with the linker.

    LinuxSmith, have a look at the ld(1) man page, either on your own system, or here. Do a search for this (read the paragraph that follows it):

    -l namespec
    --library=namespec

    Quote Originally Posted by LinuxSmith
    So where might I find a listing of all common pre-defined library names then?
    It is not so simple as that - for any library that is not standard, you will have to use the -l switch to tell the linker to look for something that is not standard. For example, when compiling OpenGL (with SDL) stuff under linux, you would see lots of -l switches:

    Code:
    -lglut -lXext -lX11 -lXmu -lXi -lGL -lGLU -lm -lSDL -lSDL_mixer
    rags_to_riches is correct though - the man pages for the functions you are using ought to tell you when you need to use a switch like that.

    As an aside, compile the following program, (using the -lm switch), and then run it with ldd.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
        double base = 2.0;
        double exp = 3.0;
        printf("2^3=%.0f\n", pow(base, exp));
        return 0;
    }
    As an example, this is what I get:

    Code:
    ~/cprogs/board> gcc -Wall -Wextra -o demo -lm demo.c
    ~/cprogs/board> ldd ./demo                          
            linux-vdso.so.1 =>  (0x00007fff5bfff000)
            libm.so.6 => /lib64/libm.so.6 (0x00007f7d9ca63000)
            libc.so.6 => /lib64/libc.so.6 (0x00007f7d9c703000)
            /lib64/ld-linux-x86-64.so.2 (0x00007f7d9ccba000)
    ~/cprogs/board>
    As you can see, my program needed libm, which is why I needed to use -lm.

    I am not sure why math.h is considered non-standard - it seems to me that this is a carry over from old Unix behaviour that was long since unnecessary. You can read a little more about some of those quirks (though no explanation as to why) in the FAQ (follow the links through as well).

    Edit: There is a discussion on stackoverflow about why we still need to manually link libm. Lots of ideas to consider, if nothing else. Read it here.
    Last edited by kermit; 08-15-2010 at 06:36 PM.

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    Agree, kermit...but linker options are shown on that page:
    Linker Options
    object-file-name -llibrary -nostartfiles -nodefaultlibs -nostdlib -pie -rdynamic -s -static -static-libgcc -shared -shared-libgcc -symbolic -Wl,option -Xlinker option -u symbol
    But you are correct...the ld man page is a better in-depth source of info on this.
    Last edited by rags_to_riches; 08-16-2010 at 06:41 AM. Reason: Fixed tags.

  10. #10
    Registered User
    Join Date
    Aug 2010
    Posts
    4
    Thanks for all the info everyone, I just love ancient computer history, and I'm not being sarcastic about that either!

    The discussion on StackOverflow was quite intriguing as well. Nothing that must resolve into binary at some point can ever be so simple, I guess, but that's what makes programming so fun!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Replies: 4
    Last Post: 09-02-2007, 08:47 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. Gnu C++ Library Header file question?
    By xddxogm3 in forum C++ Programming
    Replies: 13
    Last Post: 10-01-2003, 03:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21