c++g++yaml-cpp

yaml-cpp compile error: "undefined reference to YAML::LoadFile"


I'm trying to get a simple test program with yaml-cpp to run that looks like this:

// main.cpp

#include <iostream>
#include "yaml-cpp/yaml.h"

int main(int argc, const char *argv[])
{
     YAML::Node config = YAML::LoadFile("config.yaml");
     std::cout << config << std::endl;
}

I'm trying to compile with this command:

g++ -lyaml-cpp -L$(HOME)/local/yaml-cpp/build -I$(HOME)/local/yaml-cpp/include -std=c++11 main.cpp

and I'm getting an error that looks like this:

/tmp/ccz0D5ol.o: In function `main':
main.cpp:(.text+0xdd): undefined reference to `YAML::LoadFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1

Here's the g++ I'm using:

$ g++ --version
g++ (GCC) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

This is on a linux machine where I don't have root access. I cloned the git repo to $HOME/local/yaml-cpp, created a build directory, ran cmake -DBUILD_SHARED_LIBS=ON .., then make, which created the file libyaml-cpp.so.0.6.2 and symlinks libyaml-cpp.so.0.6 and libyaml-cpp.so. My cmake is version 3.11.2 if that matters.

Not sure if this is helpful, but I first ran this problem by the maintainer of yaml-cpp as a github issue, and he said

It's likely a configuration error; you might be able to get an answer if you ask on Stack Overflow.

I also found a similar question with pretty much the exact same problem, but her solution of rearranging the order of the compile command didn't work for me. I get the same problem whether -lyaml-cpp is towards the beginning or the end of the command.

Any help would be appreciated.


Solution

  • @VTT was right to point me towards cmake and compiler stuff. When I looked at the results of the original cmake command, the first 2 lines said

    -- The C compiler identification is GNU 4.8.5
    -- The CXX compiler identification is GNU 4.8.5
    

    Even though which gcc and which g++ point to version 5.4.0. I found this SO answer which points to this cmake FAQ page saying that you need to set the CC and CXX environment variables to specify a different compiler. So I changed my cmake command to this:

    CC=$(which gcc) CXX=$(which g++) cmake -DBUILD_SHARED_LIBS=ON ..
    

    and now everything works great.