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

jeduni

macrumors newbie
Original poster
Jul 30, 2011
2
0
Hi,

I am following the CS50 online programming course on my own and I wanted to use the CS50 library which is being used in the course. I believe I finally managed to install it properly (I was lacking gc.h obviously at first). However, when I try to compile a file from one of the lectures, Terminal returns the following:

Code:
mb:~ jdn$ gcc h1.c -lcs50
Undefined symbols:
  "_GC_malloc", referenced from:
      _GetString in libcs50.a(cs50.o)
  "_GC_free", referenced from:
      _GetString in libcs50.a(cs50.o)
      _GetString in libcs50.a(cs50.o)
      _GetString in libcs50.a(cs50.o)
  "_GC_realloc", referenced from:
      _GetString in libcs50.a(cs50.o)
ld: symbol(s) not found
collect2: ld returned 1 exit status

I am new to the world of programming, but I hope you will be able to point me towards a solution. Does it have something to do with the compiler not knowing what the function "GetString" does?
Anyway, the file I am trying to compile is this one (called h1.c):

Code:
#include <cs50.h>
#include <stdio.h>
int
main()
{
printf("State your name: ");
string name = GetString();
printf("O hai, %s!\n", name);
}

Hope you can help,
Jesper
 
Where did you get the libcs50.a file? Did you compile that yourself?

If I do this order of steps for Lecture 5 source code, I get the expected results:
Code:
$ gcc -c -ggdb -std=c99 cs50.c -o cs50.o
$ ar rcs libcs50.a cs50.o
$ gcc [COLOR="Red"]-I "."[/COLOR] h1.c [COLOR="red"]-L "."[/COLOR] -lcs50
$ ./a.out
State your name: bla
O hai, bla!
$

Note my changes in red are because I am keeping the libcs50.a and cs50.h file in the same directory as the h1.c and this tells the compiler how to find those files. If you do the above does it work?
 
GetString relies on libgc so you need to link it to your executable as well. Assuming libgc is as equally accessible as the cs50 library, then your compile command needs to be:
Code:
gcc h1.c -lcs50 lgc

@ehoui Did you use version 1 of the library that didn't require libgc?
 
Last edited:
Thanks for the answers!

@jiminaus I added -lgc to the compile command, however, Terminal returned

Code:
mb:~ jdn$ gcc h1.c -lcs50 -lgc
ld: library not found for -lgc
collect2: ld returned 1 exit status

libgc resided in /usr/lib/gc but I guess it wasn't able to find it in the gc-subfolder (I would have thought it searched subfolders too?)? Anyway I tried moving the contents of the above-mentioned folder to /usr/local/lib (the same folder as the cs50.a library) and now it seems to work!
It's just that when showing how to compile using the cs50 library in one of the lectures the teacher only added -lcs50 (not -lgc), but I guess his system must be configured in another way.

@ehoui I am using version 2.0 which I compiled following the guidelines on https://manual.cs50.net/CS50_Library.

Again, thanks a lot for the replies :)

/Jesper
 
Hello!

I'm no expert but I got it working on my Macbook pro (snow leopard) by using these commands. Basically they are the same from the site that you listed but you don't place them in the "local" folder.

gcc -c -ggdb -std=c99 cs50.c -o cs50.o
ar rcs libcs50.a cs50.o
rm -f cs50.o
chmod 0644 cs50.h libcs50.a
sudo mv cs50.h /usr/include
sudo cp libcs50.a /usr/lib
 
I downloaded my version from http://cs50.tv/2010/fall/ and it is less than 2.0. I think the sole difference is that the 1.x libraries leaked memory. I doubt this matters for running through the exercises. You won't be using cs50.c in a production environment.

Also, I gave up after spending 30 minutes trying to get libgc to compile on Lion. Moving to Lion has set me back a bit as I try to figure out how to get what used to compile to actually compile and work. Lion/Xcode 4 have apparently changed things...arg.
 
With gcc you can use the -L flag to specify a directory to search for libraries and the -I flag to tell is where to search for include files.

If you are in the same directory as the header files and libraries you want to include, then

Code:
gcc -L. -I. h1.c -lcs50 -lgc

"." means the current directory. Or you can give another path. This way you don't have to go copying files into the system areas of your disk.
 
sry to reopen this again..

sry to reopen this again, but i've tried everything and i still can't get them to work... i'm on lion and i always get this error:

Undefined symbols for architecture x86_64:
"_GetString", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
 
Last edited:
The error tells you exactly what happened:

Code:
Undefined symbols for architecture x86_64:

The linker (the thing that makes an executable from compiled code) could not find a reference to some function in some compiled 64 bit code its working with. What function is it?

Code:
"_GetString", referenced from:

Ah, some function named GetString()! The code linker can't find a 64 bit version of this. But where is the function GetString() used?

Code:
_main in main.o

Ah hah! In the function main() in the compiled object named main.o - most likely compiled code from the source file main.c.

And then a little summary of the problem:

Code:
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Now you know what happened. There is a function called GetString() that your code is trying to use in the function main() in the source file named main.c. The linker can't find it so it gives you that error and stops since there no way it can make an executable if it doesn't know where GetString() is.
 
sry to reopen this again, but i've tried everything and i still can't get them to work... i'm on lion and i always get this error:

Undefined symbols for architecture x86_64:
"_GetString", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Post the exact command-line that produced the error. "I've tried everything" is impossible for us to debug. We need to see exactly what you did. We can't read your mind, see your screen, or look at your files. If you don't give details, we can't guess them.

If you're not using the command-line, you need to tell us exactly what you're using.
 
If I'd have to guess, you either forgot to link to the cs50 library, or the cs50 library isn't 64-bit. But if cs50 wasn't 64-bit, I think the linker would have mentioned that.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.