Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

newprogramer

macrumors newbie
Original poster
Sep 17, 2009
2
0
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
 
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
 
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
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.