Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 30, 2011, 05:31 AM   #1
jeduni
macrumors newbie
 
Join Date: Jul 2011
Compiling in C using cs50 library

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
jeduni is offline   0 Reply With Quote
Old Jul 30, 2011, 06:07 AM   #2
ehoui
macrumors regular
 
Join Date: Jan 2011
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 -I "." h1.c -L "." -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?
ehoui is offline   0 Reply With Quote
Old Jul 30, 2011, 06:16 AM   #3
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
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 by jiminaus; Jul 30, 2011 at 06:19 AM. Reason: Surprise at ehoui's success
jiminaus is offline   0 Reply With Quote
Old Jul 30, 2011, 06:53 AM   #4
jeduni
Thread Starter
macrumors newbie
 
Join Date: Jul 2011
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
jeduni is offline   0 Reply With Quote
Old Jul 30, 2011, 11:06 AM   #5
perpetualpoet
macrumors newbie
 
Join Date: Sep 2009
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
perpetualpoet is offline   0 Reply With Quote
Old Jul 30, 2011, 08:55 PM   #6
ehoui
macrumors regular
 
Join Date: Jan 2011
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.
ehoui is offline   0 Reply With Quote
Old Jul 31, 2011, 01:47 AM   #7
mfram
macrumors 6502a
 
Join Date: Jan 2010
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.
mfram is offline   0 Reply With Quote
Old Aug 19, 2012, 10:59 AM   #8
captainvera
macrumors newbie
 
Join Date: Apr 2012
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 by captainvera; Aug 19, 2012 at 04:23 PM.
captainvera is offline   0 Reply With Quote
Old Aug 19, 2012, 05:02 PM   #9
softwareguy256
macrumors regular
 
Join Date: Jun 2010
dude why are you asking here, go ask the harvard prof/TA
softwareguy256 is offline   0 Reply With Quote
Old Aug 20, 2012, 02:07 PM   #10
Sayer
macrumors 6502a
 
Sayer's Avatar
 
Join Date: Jan 2002
Location: Austin, TX
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.
__________________
Obama is a true statesman whose experience as a state senator, half-term US Senator & guest lecturer in a Constitutional Law class has fully prepared him to take control of our nuclear arsenal.-Me
Sayer is offline   0 Reply With Quote
Old Aug 20, 2012, 02:50 PM   #11
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by captainvera View Post
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.
chown33 is offline   0 Reply With Quote
Old Aug 29, 2012, 06:56 PM   #12
Madd the Sane
macrumors 6502
 
Join Date: Nov 2010
Location: Utah
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.
__________________
Get out of my mind, idea! I already have an idea in here!
Madd the Sane is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Compiling on x86_64 with external libraries nh12 Mac Programming 11 Sep 1, 2011 06:31 AM
Help compiling/running graphics in terminal dflem11 Mac Programming 2 Nov 8, 2010 02:42 PM
Is it possible to use aperture library as default library in iphoto? jbg232 Digital Photography 1 Oct 24, 2010 08:36 AM
How to use c library in the iphone project? youngplayer iPhone/iPad Programming 2 Sep 20, 2009 02:00 AM
How to use .a library in iphone project? youngplayer Mac Programming 0 Sep 12, 2009 12:48 AM


All times are GMT -5. The time now is 02:54 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC