installing c libraries in Terminal

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

  1. newprogramer macrumors newbie

    Sep 17, 2009

    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>

    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?

  2. SRossi macrumors regular

    May 27, 2009
    Glasgow, Scotland
    Couple of things I noticed:

    First - Should it not be
    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.

  3. newprogramer thread starter macrumors newbie

    Sep 17, 2009
    Thanks Stephen,

    I just managed to solve the problem.

    It turns out that when compiling I typed

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

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

  4. drsoong macrumors member

    Mar 24, 2008
    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

    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