Search code examples
c++cmakeyaml-cpp

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? 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? 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.

Edit: I have not "make". How can I get this?


Solution

  • 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.

    1. go to https://github.com/jbeder/yaml-cpp and download the root library.
    2. open cmake gui and select the source directory as <my project>/yaml-cpp-master

    3. select a directory for the build. I would call it <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

    7. Compile the project as you normally would.

    8. find the generated DLL files and link them to your project.

    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:

    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.

    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.

    the .cpp .c etc is where the implementation is but .h .hpp is where the definitions are.

    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