Thread: gcc link external library

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    gcc link external library

    Code:
    [b12573@iptel7] __linux_build_tmp $ gcc -o test test.c libMGCL.a -Wall
    test.c: In function `main':
    test.c:8: warning: implicit declaration of function `yyparse'
    /usr/bin/ld: libMGCL.a(MGCL_parser.o): Relocations in generic ELF (EM: 20)
    libMGCL.a: could not read symbols: File in wrong format
    collect2: ld returned 1 exit status
    I get this error when I try to compile a .c source

    test.c

    Code:
    #include <stdio.h>
    
    
    int main()
    {
            printf("Hello world\n");
    
            yyparse();
    
            return 0;
    }
    the yyparse() is a function from libMGCL.a

    Where do I go wrong? I don't know very much about linking libraries and such!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Try including the necessary header file for the function.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > libMGCL.a: could not read symbols: File in wrong format
    This is your first big problem, where did you get that library from?

    To search a library, the command syntax would be
    gcc -o test test.c -L. -lMGCL -Wall
    The -L tells the linker where to look for more libraries
    The -l tells the linker the name of a library, in this case libMGCL.a


    Oh, and avoid calling your programs 'test', because there is a standard command called 'test' which appears to do nothing if you don't supply any command line parameters to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    is there any tool for showing me the content of a library ? some info's about it or anything ?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    The 'ar' tool will list the members of the library.
    The 'nm' tool will list all the symbols in a library / object file / executable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The command file might tell you what type of library that is.
    Oh, and avoid calling your programs 'test', because there is a standard command called 'test' which appears to do nothing if you don't supply any command line parameters to it.
    It's a bash builtin, and it never prints anything no matter what, apparently.
    Code:
    $ help test
    test: test [expr]
        Exits with a status of 0 (true) or 1 (false) depending on
        the evaluation of EXPR.  Expressions may be unary or binary.  Unary
        expressions are often used to examine the status of a file.  There
        are string operators as well, and numeric comparison operators.
    
        File operators:
    
            -a FILE        True if file exists.
            -b FILE        True if file is block special.
            -c FILE        True if file is character special.
            -d FILE        True if file is a directory.
            -e FILE        True if file exists.
            -f FILE        True if file exists and is a regular file.
            -g FILE        True if file is set-group-id.
            -h FILE        True if file is a symbolic link.
            -L FILE        True if file is a symbolic link.
            -k FILE        True if file has its `sticky' bit set.
            -p FILE        True if file is a named pipe.
            -r FILE        True if file is readable by you.
            -s FILE        True if file exists and is not empty.
            -S FILE        True if file is a socket.
            -t FD          True if FD is opened on a terminal.
            -u FILE        True if the file is set-user-id.
            -w FILE        True if the file is writable by you.
            -x FILE        True if the file is executable by you.
            -O FILE        True if the file is effectively owned by you.
            -G FILE        True if the file is effectively owned by your group.
            -N FILE        True if the file has been modified since it was last read.
    
          FILE1 -nt FILE2  True if file1 is newer than file2 (according to
                           modification date).
    
          FILE1 -ot FILE2  True if file1 is older than file2.
    
          FILE1 -ef FILE2  True if file1 is a hard link to file2.
    
        String operators:
    
            -z STRING      True if string is empty.
    
            -n STRING
            STRING         True if string is not empty.
    
            STRING1 = STRING2
                           True if the strings are equal.
            STRING1 != STRING2
                           True if the strings are not equal.
            STRING1 < STRING2
                           True if STRING1 sorts before STRING2 lexicographically.
            STRING1 > STRING2
                           True if STRING1 sorts after STRING2 lexicographically.
    
        Other operators:
    
            -o OPTION      True if the shell option OPTION is enabled.
            ! EXPR         True if expr is false.
            EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
            EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
    
            arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
                           -lt, -le, -gt, or -ge.
    
        Arithmetic binary operators return true if ARG1 is equal, not-equal,
        less-than, less-than-or-equal, greater-than, or greater-than-or-equal
        than ARG2.
    $
    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.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Salem View Post
    To search a library, the command syntax would be
    gcc -o test test.c -L. -lMGCL -Wall
    The -L tells the linker where to look for more libraries
    The -l tells the linker the name of a library, in this case libMGCL.a
    Listing the .a file directly on the link line is also perfectly legitimate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's an import library?
    By chiefmonkey in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2009, 05:00 PM
  2. Property Set Library (PSL) - Announcement
    By vultur_gryphus in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 05-29-2008, 06:04 AM
  3. Problem calling external method from within a shared library
    By Major Tom in forum C++ Programming
    Replies: 0
    Last Post: 04-21-2007, 09:14 AM
  4. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  5. Link list library
    By Brighteyes in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 08:49 PM