设置库包含 C++ 中的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2496950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setting library include paths in c++
提问by Drew
I just installed gd2 using mac ports (sudo install gd2), which installed libraries in the following places:
我刚刚使用mac端口安装了gd2(sudo install gd2),它在以下位置安装了库:
/opt/local/include/gd.h
/opt/local/lib/libgd.dylib (link)
/opt/local/lib/libgd.la
/opt/local/lib/libgd.a
Here is my make file also:
这也是我的make文件:
dev: main.o
g++ -L/opt/local/lib -I/opt/local/include -lgd -lpng -lz -ljpeg -lfreetype -lm main.o -o heatmap
main.o: main.cpp
g++ -c main.cpp
So when I create my c++ app I add '#include "gd.h"', which throws:
所以当我创建我的 C++ 应用程序时,我添加了“#include“gd.h””,它抛出:
main.cpp:4:16: error: gd.h: No such file or directory
If I set gd.h as an absolute path (as above)(not a solution, but was curious), I am thrown:
如果我将 gd.h 设置为绝对路径(如上所述)(不是解决方案,但很好奇),我会被抛出:
g++ -L/opt/local/include -L/opt/local/lib main.o -o heatmap
Undefined symbols:
"_gdImagePng", referenced from:
_main in main.o
"_gdImageLine", referenced from:
_main in main.o
"_gdImageColorAllocate", referenced from:
_main in main.o
_main in main.o
"_gdImageDestroy", referenced from:
_main in main.o
"_gdImageCreate", referenced from:
_main in main.o
"_gdImageJpeg", referenced from:
_main in main.o
ld: symbol(s) not found
So, I understand this means that ld can not find the libraries it needs (hence trying to give it hints with the "-L" values). So after giving g++ the -L hints and the absolute path in #include, I can get it to work, but I don't think I have to do this, how can I make g++/ld search int eh right places for the libraries?
所以,我理解这意味着 ld 找不到它需要的库(因此试图用“-L”值给它提示)。所以在给 g++ -L 提示和 #include 中的绝对路径之后,我可以让它工作,但我认为我不必这样做,我怎样才能让 g++/ld search int eh 为库的正确位置?
Drew J. Sonne.
德鲁·J·索恩。
PS. using: - OSX 10.6.2 - gcc version 4.2.1 (Apple Inc. build 5646) (dot 1)
附注。使用: - OSX 10.6.2 - gcc 版本 4.2.1 (Apple Inc. build 5646) (dot 1)
EDIT:
Ok, so after taking into account stfanB and Michael's answer, I've recompiled gd into a local directory (libraries
) and thus, I've changed that first line of my Makefile (I will def check out cmake) to g++ -L./libraries/lib -I./libraries/include -lgd -lpng -lz -ljpeg -lfreetype -lm main.o -o heatmap
But I'm still getting main.cpp:3:16: error: gd.h: No such file or directory
编辑:好的,考虑到 stfanB 和 Michael 的回答后,我将 gd 重新编译到本地目录 ( libraries
) 中,因此,我已将我的 Makefile 的第一行(我将 def check out cmake)更改为g++ -L./libraries/lib -I./libraries/include -lgd -lpng -lz -ljpeg -lfreetype -lm main.o -o heatmap
But I'我还在main.cpp:3:16: error: gd.h: No such file or directory
EDIT: Thanks all for the answers, here's my final (working) makefile for anyone else who many want an answer:
编辑:感谢所有人的答案,这是我为许多想要答案的其他人准备的最终(工作)makefile:
dev: main.o
g++ -I./libraries/include -L./libraries/lib -lgd -lpng -lz -ljpeg -lfreetype -lm main.o -o heatmap
main.o: main.cpp
g++ -I./libraries/include -c main.cpp
回答by Michael Aaron Safyan
Rather than invoke g++ directly, I strongly advise you to use CMake(watch the CMake Google Techtalkif you'd like to learn more) as it will make your life way easier and greatly simplifies locating and linking against a variety of libraries. That said, I believe the problem with your invocation is that you have not specified the library, which you would do with -lgd
. Your -L/opt/local/lib
correctly tells g++ to look in /opt/local/lib
, but you never told it what to look for. As for finding the appropriate header, you can use -I/opt/local/include
to put /opt/local/include
in the compiler's include search path.
我强烈建议您不要直接调用 g++,而是强烈建议您使用CMake(如果您想了解更多信息,请观看CMake Google Techtalk),因为它会让您的生活更轻松,并极大地简化了针对各种库的定位和链接。也就是说,我相信您调用的问题在于您没有指定库,而您将使用-lgd
. 您-L/opt/local/lib
正确地告诉 g++ 查看/opt/local/lib
,但您从未告诉它要查找的内容。至于找到合适的头文件,你可以使用-I/opt/local/include
放入/opt/local/include
编译器的包含搜索路径。
If you were to heed my advice to use CMake, doing this would look like:
如果你听从我的建议来使用 CMake,这样做看起来像:
FIND_PACKAGE(GD2 REQUIRED) INCLUDE_DIRECTORIES(${GD2_INCLUDE_DIRS}) LINK_DIRECTORIES(${GD2_LIBRARY_DIRS}) ADD_EXECUTABLE(heatmap main Heatmap_Map Heatmap_Point) TARGET_LINK_LIBRARIES(heatmap ${GD2_LIBRARIES})
If you are interested in learning more about CMake, you might want to take a look at the C++ Application Project Templateand C++ Library Project Template, which make use of the CMake build system. CMake is available through MacPorts via the command "sudo port install cmake".
如果您有兴趣了解有关 CMake 的更多信息,您可能需要查看使用 CMake 构建系统的C++ 应用程序项目模板和C++ 库项目模板。CMake 可通过 MacPorts 通过命令“sudo port install cmake”获得。
In case you are not interested in installing CMake, I should also point out that there are some environment variables that you might be interested in knowing about to make your life easier, namely:
如果您对安装 CMake 不感兴趣,我还应该指出,您可能有兴趣了解一些环境变量,以便让您的生活更轻松,即:
- CPATH
- LIBRARY_PATH
- DYLD_FALLBACK_LIBRARY_PATH
- 路径
- LIBRARY_PATH
- DYLD_FALLBACK_LIBRARY_PATH
The CPATH environment variable is much like the PATH environment variable (it is a colon separated list of directories), except that the directories in that variable will automatically be used by gcc and g++ as if they were specified on the commandline with the -I
flag (i.e. headers will be searched in those paths). The LIBRARY_PATH is the equivalent except that it is as if the folders were given with -L
(i.e. libraries will automatically be searched in that path). DYLD_FALLBACK_LIBRARY_PATH will be used by the dynamic linker (so you should probably include the paths from LIBRARY_PATH into this variable).
CPATH 环境变量很像 PATH 环境变量(它是一个以冒号分隔的目录列表),不同之处在于该变量中的目录将被 gcc 和 g++ 自动使用,就好像它们是在命令行上用-I
标志指定的一样(即将在这些路径中搜索标题)。LIBRARY_PATH 是等效的,除了它就像文件夹被赋予一样-L
(即库将自动在该路径中搜索)。DYLD_FALLBACK_LIBRARY_PATH 将由动态链接器使用(因此您可能应该将来自 LIBRARY_PATH 的路径包含到此变量中)。
You can read more about the environment variables affecting gccat the link.
回答by stefanB
The answer is quite complicated.
答案相当复杂。
The short answer, when you compile your own libs/tools place them in some local
directory such as you used above or /usr/local/lib
and /usr/local/include
or even ~/local/lib
and ~/local/include
and always add these to your compiler/linker.
简短的回答,当您编译自己的库/工具时,将它们放在某个local
目录中,例如您在上面使用的或/usr/local/lib
和/usr/local/include
或什~/local/lib
至~/local/include
和并始终将它们添加到您的编译器/链接器中。
The longer answer - read Programming Library HOWTOfor Linux that explains what each of the tool involved looks for, from compiler/linker to execution, and have a look at standard directory structure of Linux systemwhich is of course not binding but it's nice to know.
更长的答案 - 阅读Linux 的编程库 HOWTO,它解释了所涉及的每个工具的查找内容,从编译器/链接器到执行,并查看Linux 系统的标准目录结构,这当然没有约束力,但很高兴知道.
I'm assuming that you have a simple Makefile setup for each of your projects so you don't have to bother with typing all those commands. If you don't I strongly recommend to setup a simple Makefile template that you can reuse for your projects.
我假设您的每个项目都有一个简单的 Makefile 设置,因此您不必费心输入所有这些命令。如果你不这样做,我强烈建议你设置一个简单的 Makefile 模板,你可以在你的项目中重复使用它。
EDIT:
编辑:
In your edited answer your paths may be incorrect.
在您编辑的答案中,您的路径可能不正确。
The .
in your path like ./libraries
specifies current directory, meaning it will look into libraries
directory in the current directory. If you ment libraries
directory in root directory then remove the .
so it should be like this -L/libraries/lib
and similarly for -I/libraries/...
在.
您的路径就像./libraries
指定当前目录,这意味着它会考虑libraries
目录在当前目录。如果您libraries
在根目录中指定目录,则删除.
它,因此它应该是这样的-L/libraries/lib
,对于-I/libraries/...
回答by dash-tom-bang
I think your problem may be that your -I arguments need to be passed to the compile stage, too. Maybe something like this?
我认为您的问题可能是您的 -I 参数也需要传递到编译阶段。也许是这样的?
dev: main.o
g++ -L/opt/local/lib -lgd -lpng -lz -ljpeg -lfreetype -lm main.o -o heatmap
main.o: main.cpp
g++ -I/opt/local/include -c main.cpp
Not sure- I haven't used makefiles in 20 years (but as I mentioned in a comment to another post, I find JamPlus quite remarkable in its power).
不确定——我已经 20 年没有使用过 makefile(但正如我在另一篇文章的评论中提到的,我发现 JamPlus 的功能非常出色)。
Anyway- typically your compiler and linker flags will be put into variables and then expanded in the command line, but I'm not sure on the make syntax for that. Maybe simply:
无论如何 - 通常你的编译器和链接器标志将被放入变量中,然后在命令行中展开,但我不确定它的 make 语法。也许简单:
CFLAGS=-I/opt/local/include
LINKFLAGS=-L/opt/local/lib -lgd -lpng -lz -ljpeg -lfreetype -lm
dev: main.o
g++ $(LINKFLAGS) main.o -o heatmap
main.o: main.cpp
g++ $(CFLAGS) -c main.cpp
回答by Sameer
Header include directories need to be specified with -I option (only relative paths needed after this) and linker include directories with -L (relative paths here too).
头文件包含目录需要用 -I 选项指定(此后只需要相对路径),链接器用 -L 包含目录(这里也是相对路径)。
For libraries, an alternative you could alternatively set the LD_LIBRARY_PATH but the -L method is safer.
对于库,您可以选择设置 LD_LIBRARY_PATH 但 -L 方法更安全。
回答by santeri
You need to use -iquote when using #include "gd.h", see the gcc directory options documentation.
使用 #include "gd.h" 时需要使用 -iquote,请参阅gcc 目录选项文档。
Also, specifying the include directory in your link stage is pointless.
此外,在链接阶段指定包含目录是没有意义的。