简体   繁体   English

如何使用cmake在我的代码中包含一个库?

[英]How to include a library in my code using cmake?

I have tried to ´make´ the library yaml-cpp, not sure I did it right, but how do I build it? 我试图“制作” yaml-cpp库,但不确定是否正确,但是如何构建它? In the tutorial ( https://github.com/jbeder/yaml-cpp/blob/master/README.md ) it says to run cmake in the build dir, but cmake could not find the cmakelist file, so I did it in the source dir, but then what? 在教程( https://github.com/jbeder/yaml-cpp/blob/master/README.md )中,它说在构建目录中运行cmake,但是cmake找不到cmakelist文件,所以我在源目录,但是那又如何呢? How do I build it? 我该如何建造?

If someone could make a newbie step by step to get the library (or any library really) so I can include it in my code, that would be awesome. 如果有人可以一步一步地获得一个新手来获取库(或真正的任何库),以便我可以将其包含在我的代码中,那就太好了。

Im using Windows 7, and compiling using terminal (using Codeblocks MinGW gcc/g++) and sublime text 3 editor. 我使用Windows 7,并使用终端(使用Codeblocks MinGW gcc / g ++)和Sublime Text 3编辑器进行编译。

Edit: I have not "make". 编辑:我还没有“使”。 How can I get this? 我怎么能得到这个?

Here is the step by step guide: For the purpose of this answer I will use cmake gui instead to highlight a few key points. 这是逐步指南:针对此答案,我将使用cmake gui突出显示一些关键点。

  1. go to https://github.com/jbeder/yaml-cpp and download the root library. 转到https://github.com/jbeder/yaml-cpp并下载根库。
  2. open cmake gui and select the source directory as <my project>/yaml-cpp-master 打开cmake gui并将源目录选择<my project>/yaml-cpp-master

  3. select a directory for the build. 选择构建目录。 I would call it <my project>/yaml-cpp-master/codeblocks_build 我将其称为<my project>/yaml-cpp-master/codeblocks_build

  4. press configure and then check all the values. 按配置,然后检查所有值。

  5. press generate and wait for it to complete. 按生成并等待其完成。

  6. Find the generated codeblocks project file within <my project>/yaml-cpp-master/codeblocks_build <my project>/yaml-cpp-master/codeblocks_build找到生成的代码块项目文件

  7. Compile the project as you normally would. 照常编译项目。

  8. find the generated DLL files and link them to your project. 找到生成的DLL文件并将其链接到您的项目。

The reason why you are getting this error is because cmake is trying to find the source code in the directory build which is newly created as seen in the tutorial: 收到此错误的原因是因为cmake试图在新创建的目录build找到源代码,如本教程所示:

mkdir build
cd build

This is meant to specify to cmake where to build it in rather than where to build from. 这是为了指定在哪里构建而不是从哪里构建。 If you wish to use it via a command line you will need to tell cmake where to build and where the source is. 如果您希望通过命令行使用它,则需要告诉cmake在何处构建以及源在哪里。

To then call the functions from that library you will need to link the header files (files that start with .h or .hpp ) and the DLL libraries. 要从该库中调用函数,您需要链接头文件(以.h.hpp开头的文件)和DLL库。

the .cpp .c etc is where the implementation is but .h .hpp is where the definitions are. .cpp .c etc是实现的位置,而.h .hpp是定义的位置。

So when you are including like this: #include<something.h> you are including definitions which are later filled by the .cpp files however in case of a library they are instead filled from .dll or .o 因此,当您包含这样的内容: #include<something.h>您包含的定义随后将由.cpp文件填充,但是对于库,则改为由.dll.o填充。

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

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