简体   繁体   English

如何在我的包含路径中添加目录,以避免用C ++编写完整的目录路径?

[英]How to add a directory to my include path to avoid writing the complete directory path in C++?

I'm pretty new to programming and want to use opencv2. 我对编程很陌生,想使用opencv2。 I've got a code package and want to run it. 我有一个代码包,想要运行它。 It is written in C++ and I want to include several files within opencv2 and the code is: 它是用C ++编写的,我想在opencv2中包含几个文件,代码是:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"

My question is why didn't #include give the whole directory like /usr/local/include/opencv2/... ? 我的问题是为什么#include不给整个目录如/usr/local/include/opencv2/...

This code could not run on my computer because I didn't add opencv2 into my include path. 该代码无法在我的计算机上运行,​​因为我没有将opencv2添加到我的包含路径中。

What should I do? 我该怎么办?

If your compiler does not automatically look for headers under /usr/local/include , then you need to tell it to do so: 如果您的编译器没有自动在/usr/local/include下查找标头,那么您需要告诉它这样做:

g++ … -I/usr/local/include … source.cpp

The reason that you don't put the full path name into the source is that it makes it unportable. 您未将完整路径名放入源中的原因是它使其不可移植。 You have OpenCV2 installed in /usr/local , I have it in /opt/OpenCV2 . 您在/usr/local安装了OpenCV2,在/opt/OpenCV2安装了它。 The source would have to be hacked horribly if the code said: 如果代码说明,则必须骇人地破解源代码:

#include "/usr/local/include/opencv2/core/core.hpp"

The way it is, people can locate the OpenCV2 library and supporting headers in any location and only have to specify that location on the command line (or in the makefile, or let an auto-configuring tool do the work for you). 这样,人们可以在任何位置找到OpenCV2库和支持标头,而只需在命令行上(或在makefile中,或让自动配置工具为您完成)指定该位置。

Add OpenCV2 into your include path. 将OpenCV2添加到您的包含路径中。

You can write an absolute path into your #include directive if you want, but it's sorely frowned upon as it makes your code utterly non-portable. 您可以根据需要在#include指令中写入一个绝对路径,但由于它使您的代码完全不可移植,因此非常恼火。

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

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