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.

4

1 回答 1

2

该链接器错误通常意味着头文件被包含在内,但是库却不包含在内。我不知道 Clang,所以不能给你一个完整的答案,但是对于 g++,你需要找到库文件并确保它的目录在你的 LD_LIBRARY_PATH 中,然后取它的名字;它将类似于“lib StanfordCPP .so”。然后,您需要在命令行上的链接器中添加一个标志,该标志在 -l(连字符-L)之后包含名称的粗体部分(在 lib 和文件扩展名之间),所以这里将是-lStanfordCPP.

然后你的命令行看起来像:

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

不幸的是,这不是 Clang,但它应该可以正常工作。如果其他人想提供 Clang 方式(或验证您可以使用 Clang 执行此操作),那就太好了。

于 2013-09-05T23:00:16.097 回答