installing c libraries in Terminal

Discussion in 'Mac Programming' started by newprogramer, Sep 17, 2009.

  1. newprogramer macrumors newbie

    Joined:
    Sep 17, 2009
    #1
    Hi,

    I'm following an online computer science course which requires me to compile a static library (cs50.h) on my own system which provides additional functionality not included in stdio.h

    here are the commands I used to do so

    gcc -c -ggdb -std=c99 cs50.c -o cs50.o
    ar rcs libcs50.a cs50.o
    rm -f cs50.o
    sudo cp cs50.h /usr/local/include
    sudo libcs50.a /usr/local/lib

    these commands ran with no errors

    I then tried to compile the following source code

    #include <cs50.h>
    #include <stdio.h>

    int
    main (int argc, char * argv[])
    {
    printf("Please enter your name: ");
    string name = GetString();
    printf("Pleased to meet you, %s\n", name);
    }



    I received the following error message:

    Undefined symbols:
    "_GetString", referenced from:
    _main in ccqHXJVw.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

    Could anyone tell me what this error message refers to and point me in the right direction as to where a mistake could have occurred?

    Thanks
     
  2. SRossi macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #2
    Couple of things I noticed:

    First - Should it not be
    Code:
    int main(int argc, char *argv[])
    Maybe you have wrote it down incorrectly?

    Second - I do not believe that you can have a string in C you can really only use chars? Is this a function that your new library provides?

    Hope this helps.

    Stephen
     
  3. newprogramer thread starter macrumors newbie

    Joined:
    Sep 17, 2009
    #3
    Thanks Stephen,

    I just managed to solve the problem.

    It turns out that when compiling I typed

    Code:
    gcc howdy3.c
    instead I needed to link to the library cs50.c by entering the following

    Code:
    gcc howdy3.c -lcs50.c
    Thanks for your help :D

    Pete
     
  4. drsoong macrumors member

    Joined:
    Mar 24, 2008
    Location:
    Munich
    #4
    You must link with the library too

    The error "no symbol(s) not found" means that the compiler actually manages to compile your program, but the object code containing the libcs50.a's (binary) functions can't be found.

    You must add

    Code:
    -lcs50
    
    to your compiler call, so that your program is linked with the library (which is searched for in /usr/lib). That should do the job.
     

Share This Page