C++ 使用 makefile 链接到库(newbe)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4014967/
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
C++ linking to libraries with makefile (newbe)
提问by Sebastian Dusza
I'm trying to understand how to use non standard libraries in my C++ projects. I have a few questions.
我试图了解如何在我的 C++ 项目中使用非标准库。我有几个问题。
Lets say I want to use POCO library. So I downloaded it and build it using make (static build). Now I have bunch of .o files and .h files. There is a Path.h file and a Path.o file in different directories.
假设我想使用 POCO 库。所以我下载了它并使用 make(静态构建)构建它。现在我有一堆 .o 文件和 .h 文件。不同目录下有一个Path.h文件和一个Path.o文件。
Now I want to use this module in my code. So i include the file using #include "Poco/Path.h". Do I have to modify makefile and add Path.o to my target ?
现在我想在我的代码中使用这个模块。所以我使用 #include "Poco/Path.h" 包含文件。我是否必须修改 makefile 并将 Path.o 添加到我的目标?
What happens when I use standard library ? Are those available only in header files ? I know that template code cannot be precompiled. What about the rest ?
当我使用标准库时会发生什么?那些只在头文件中可用吗?我知道模板代码不能被预编译。剩下的呢?
回答by Bart van Ingen Schenau
Besides the .h and .o files, you will probably also have one or more libXXX.a
and/or libXXX.so
files. These are the actual library files that your application should link against.
除了 .h 和 .o 文件,您可能还有一个或多个libXXX.a
和/或libXXX.so
文件。这些是您的应用程序应该链接的实际库文件。
To use the library, you include the relevant headers in your source file, and you change your makefile to tell the linker that it should also link your application to the XXX library.
The typical linker-command for that is -lXXX
and the linker will look for both libXXX.a and libXXX.so and use whichever seems most appropriate.
要使用该库,您需要在源文件中包含相关的头文件,并更改您的 makefile 以告诉链接器它也应该将您的应用程序链接到 XXX 库。典型的链接器命令是-lXXX
,链接器将同时查找 libXXX.a 和 libXXX.so 并使用看起来最合适的那个。
The standard library is not really different from external libraries, except that you don't have to specify it explicitly to the linker.
标准库与外部库并没有真正的不同,只是您不必向链接器显式指定它。
回答by Beta
Your question seems to imply that you already have a makefile for your own code. If that's the case, then yes, you should modify the rule for your executable in that makefile. As Bart van Ingen Schenau points out, the POCO makefile probably assembled the objects files into libraries such as Poco/Libraries/libPoco.a
, so you should use them instead of trying to pick out the object files you need. For instance, if right now your rule reads:
您的问题似乎暗示您已经为自己的代码创建了一个 makefile。如果是这种情况,那么是的,您应该修改该 makefile 中可执行文件的规则。正如 Bart van Ingen Schenau 指出的那样,POCO makefile 可能将对象文件组装到诸如Poco/Libraries/libPoco.a
. 例如,如果现在你的规则是:
foo: foo.o bar.o
g++ -lSomeLibrary $^ -o $@
you should change it to
你应该把它改成
foo: foo.o bar.o
g++ -lSomeLibrary -LPoco/Libraries -lPoco $^ -o $@
(The second part of your question, "What happens... What about the rest?" is unclear to me.)
(你问题的第二部分,“会发生什么......剩下的呢?”我不清楚。)
Note:It's a bad idea to #include "Poco/Path.h"
. This makes your code dependent on a directory structure, something it should not care about. It is much better to #include "Path.h"
and tell the compiler where to find it: g++ -c -IPoco ...
.
注意:这是一个坏主意#include "Poco/Path.h"
。这使您的代码依赖于目录结构,这是它不应该关心的。最好#include "Path.h"
告诉编译器在哪里找到它:g++ -c -IPoco ...
.