PDA

View Full Version : installing c libraries in Terminal




newprogramer
Sep 17, 2009, 09:05 AM
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



SRossi
Sep 17, 2009, 09:52 AM
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.

Stephen

newprogramer
Sep 17, 2009, 10:08 AM
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

Pete

drsoong
Sep 17, 2009, 10:09 AM
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


-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.