How to compile using library

Discussion in 'C Programming' started by stachurr@iem.pw.edu.pl, Dec 13, 2005.

  1. Guest

    Hello

    I'm new in C, And I want to compile program which use extern function.
    How to do it because I always got error.
    Undefined first referenced
    symbol in file
    greet /tmp/cchAXHfp.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status

    first I did "gcc -G -o libgreet.so greet.c" and it is ok.
    Later I tried:
    gcc -G -lgreet mix.c
    gcc -L/local/data02/app/repapp/test/gp
    -I/local/data02/app/repapp/test/gp mix.c
    gcc -L/local/data02/app/repapp/test/gp mix.c

    And always I got error.

    Thx
    Robert

    bash-2.05$ cat greet.h
    char *greet();

    bash-2.05$ cat greet.c
    #include "greet.h"

    char *greet() {
    return ((char *) "Hello!");
    }


    bash-2.05$ cat mix.c
    #include <stdio.h>
    extern char *greet();
    int main() {
    char *greeting = greet();
    printf ("aa\n");
    return (0);
    }
     
    , Dec 13, 2005
    #1
    1. Advertising

  2. Guest

    > I'm new in C, And I want to compile program which use extern function.
    > How to do it because I always got error.
    > Undefined first referenced
    > symbol in file
    > greet /tmp/cchAXHfp.o
    > ld: fatal: Symbol referencing errors. No output written to a.out
    > collect2: ld returned 1 exit status
    >
    > first I did "gcc -G -o libgreet.so greet.c" and it is ok.
    > Later I tried:
    > gcc -G -lgreet mix.c
    > gcc -L/local/data02/app/repapp/test/gp
    > -I/local/data02/app/repapp/test/gp mix.c
    > gcc -L/local/data02/app/repapp/test/gp mix.c


    <-- snip code -->

    Simply linking both files to one executable :

    $ gcc -c greet.c -o greet.o # compile file 1
    $ gcc -c mix.c -o mix.o # compile file 2
    $ gcc mix.o greet.o -o greet # link both object files into binary
    $ ./greet # run binary



    --
    :wq
    ^X^Cy^K^X^C^C^C^C
     
    , Dec 13, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    842
  2. Nagaraj
    Replies:
    1
    Views:
    901
    Lionel B
    Mar 1, 2007
  3. timjowers
    Replies:
    7
    Views:
    4,400
    heyjude
    Feb 2, 2011
  4. Replies:
    1
    Views:
    462
    Cowboy \(Gregory A. Beamer\)
    Apr 11, 2008
  5. Carter
    Replies:
    2
    Views:
    524
    Carter
    Mar 4, 2009
Loading...

Share This Page