简体   繁体   English

如何将.cpp文件链接到.h文件以获取下载的资源库

[英]How to link .cpp file to .h file for a downloaded library

I am very new to C++, and I have been completing the exercises for the Stanford 106B CS class. 我是C ++的新手,我一直在完成斯坦福106B CS课程的练习。 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. Stanford 106B类使用我下载的StanfordCPPlib或Stanford c ++库。 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. 我正在尝试完成一个需要#include“random.h”的练习,所以我可以使用一种方法来找到0到1之间的随机实数。无论如何,只需编写#include“random.h”,其余的文本文件中的必要代码不起作用。 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. 斯坦福大学的课程使用Xcode,从网站下载的家庭作业已经设置了与斯坦福大学图书馆一起运行的Xcode项目。 There is also a blank Xcode project template to use for the text book exercises. 还有一个空白的Xcode项目模板用于教科书练习。 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. 我已经尝试编写#include“file_path_to_stanford_libraries / random.h”,我甚至不确定它是否在.cpp文件中工作,但我试过它无济于事。 I tried putting the files in the exact same directory as my random.cpp file, which also didn't work. 我尝试将文件放在与random.cpp文件完全相同的目录中,这也无法正常工作。 Sorry for this long, and hopefully, not inane post. 对不起这个漫长的,希望,不是愚蠢的帖子。 I appreciate any help. 我感谢任何帮助。

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; 我不知道Clang,所以不能给你一个完整的答案,但是对于g ++,你需要找到库文件并确保它的目录在你的LD_LIBRARY_PATH中,然后取其名字; it will be something like "lib StanfordCPP .so". 它将类似于“lib StanfordCPP .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 . 然后,您需要在命令行上向链接器添加一个标志,该标志包含在-l(连字符-L)之后的名称的粗体部分(在lib和文件扩展名之间),因此这里将是-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. 不幸的是,这不是Clang,但应该大致相同。 If someone else would like to provide the Clang way (or verify you can do this with Clang) that'd be great. 如果其他人想提供Clang方式(或验证你可以用Clang做到这一点)那就太棒了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM