0

I am very new to C++, and I have been completing the exercises for the Stanford 106B CS class. I've found a lot of posts here and elsewhere with a similar problem to mine, but none that could help me out with my specific issue. The Stanford 106B class uses the StanfordCPPlib, or Stanford c++ libraries, which I downloaded. I am trying to complete an exercise that requires me to #include "random.h" so I can use a method for finding a random real number between 0 and 1. Anyways, simply writing #include "random.h" and the rest of the necessary code in text file doesn't work. I am getting this error:

make random
c++     random.cpp   -o random
Undefined symbols for architecture x86_64:
  "randomReal(double, double)", referenced from:
      _main in random-BBexsD.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [random] Error 1

The Stanford class uses Xcode, and the homework assignments downloaded from the website have Xcode projects already set up to run with the Stanford libraries. There is also a blank Xcode project template to use for the text book exercises. However, I am trying to figure out how to do this in a text editor or terminal or both. I usually write all my code using a text editor and executing it in terminal. I have tried writing #include "file_path_to_stanford_libraries/random.h", which I'm not even sure works in a .cpp file, but I tried it to no avail. I tried putting the files in the exact same directory as my random.cpp file, which also didn't work. Sorry for this long, and hopefully, not inane post. I appreciate any help.

SherMM
  • 619
  • 6
  • 14

1 Answers1

2

That linker error normally means that the header file is being included just fine, but the library however, isn't included. I don't know Clang, so can't give a full answer for you, but for g++, you'd need to find the library file and make sure its directory is in your LD_LIBRARY_PATH, then take its name; it will be something like "libStanfordCPP.so". You then need add a flag to the linker on the command line that contains the bolded part of the name (between lib and file extension) after -l (hyphen-L), so here that would be -lStanfordCPP.

Your command line then looks something like:

g++ -o bin/random random.cpp -lStanfordCPP

Unfortunately this isn't Clang, but it ought to work about the same. If someone else would like to provide the Clang way (or verify you can do this with Clang) that'd be great.

dwarduk
  • 919
  • 1
  • 5
  • 8
  • Clang's syntax is mostly compatible with GCC. – Siyuan Ren Sep 06 '13 at 00:21
  • How can I make sure that the library file's directory is in the LD_LIBRARY_PATH? Sorry, but I am a bit of a novice with using external libraries. – SherMM Sep 11 '13 at 23:44
  • In the terminal: `export LD_LIBRARY_PATH=/absolute/path/to/directory:$LD_LIBRARY_PATH` – dwarduk Sep 12 '13 at 01:13
  • I found this link: [here](http://lists.cs.uiuc.edu/pipermail/cfe-dev/2011-December/019192.html). Seems to be written in for shell/windows command prompt. I am kinda wary about typing anything I can't change into terminal, so I just wanted to make sure about that. – SherMM Sep 12 '13 at 01:43
  • It's a temporary thing - just sets a variable, and closing and reopening the terminal undoes it (at least in Debian Linux. I assume OS X is the same). Anyway, I just had a look at the OS X documentation, and I think you can add this to the line in your makefile that calls clang: `-L/absolute/path/to/directory`. – dwarduk Sep 12 '13 at 01:55
  • If I have a really simple makefile, with one .cpp file, would i just add a line that is something like: g++ -c filename -L/path/to/lib -l/lib? I tried something similar to [this](http://stackoverflow.com/questions/12054858/add-so-and-a-libraries-to-makefile), but still no luck. – SherMM Sep 27 '13 at 02:14
  • 1
    Close! -c compiles to an object file rather than an executable; to link in the same step, you'd leave that out and use -o outputfile instead. Also the library linking is a little bit off, too. Say your library was /usr/lib/libMyLibrary.so, then you'd have this: `g++ -o myprogram -L/usr/lib/ -lMyLibrary filename.cpp` – dwarduk Sep 27 '13 at 23:17
  • Hmmm. When I tried that, I got errors along the lines of "no such file as random.h". It's not finding the .h file in the library – SherMM Oct 12 '13 at 00:20
  • What I am trying to run is one .cpp file. It includes two .h files from the Stanford library. I can't even get as far as the object file, because when I try g++ -c "library linking steps", I get the errors that both .h files don't exist. I have tried copying the .h files and placing them in the same directory as the .cpp file, and following the library link methods you list, but still no luck. – SherMM Oct 12 '13 at 02:45