5

I'm trying to use the new version of libyaml-cpp and having linker problems (undefined reference to 'YAML::LoadFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)').

I build the library as follows:

cmake -DBUILD_SHARED_LIBS=ON ..
make
sudo make install

Then I include yaml-cpp/yaml.h and call YAML::LoadFile( some_string );. My compilation line is:

g++ -L/usr/local/lib -I/usr/local/include -lyaml-cpp -std=c++0x -o $@  $^

I've tried putting the exact .so file in there as well with no luck. Using nm I can see a LoadFile function in the shared library. I can't figure out now if I'm somehow using the wrong build line or there is something wrong with the library.

edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267

6 Answers6

10

It's an ordering problem on the command-line. I guess I'll just never understand GCC command-line logic. Simply putting the library at the end seems to work:

g++ -L/usr/local/lib -I/usr/local/include -std=c++0x -o $@  $^ -lyaml-cpp
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
2

Support new yaml-cpp API.

find_package(PkgConfig)
pkg_check_modules(YAMLCPP REQUIRED yaml-cpp>=0.5)
include_directories(${YAMLCPP_INCLUDE_DIRS})

add_executable(name src/name.cpp)
target_link_libraries(name ${catkin_LIBRARIES}  ${YAMLCPP_LIBRARIES})
DrGodCarl
  • 1,666
  • 1
  • 12
  • 17
  • 1
    Could you explain what the function/goal of the steps are so that people coming to it can more easily understand it? – DrGodCarl May 27 '19 at 20:40
1

I fixed the issue by adjusting my CMakeList.txt

I had no issues building like stated above:

 g++ -L/usr/local/lib -I/usr/local/include -std=c++0x main.cpp -lyaml-cpp

But using cmake and building via CLion failed for me.

This CMakeList.txt fixed it for me (this is just a minimal stripped down Version, but it should give an idea). It assumes there is only one version of yaml-cpp installed on your system:

# Projekt Description / etc
cmake_minimum_required(VERSION 3.14)
project(yaml_cpp)
set(CMAKE_CXX_STANDARD 14)

# Declaration of package required
find_package(PkgConfig)
pkg_check_modules(YAMLCPP REQUIRED yaml-cpp>=0.5)

# Define the executable and link the yaml libs
add_executable(yaml_cpp main.cpp)
target_link_libraries(yaml_cpp ${YAMLCPP_LIBRARIES})

This was the most minimalistic way of getting things to work. I was inspired by Sinaí Aranda above.

LeTigre
  • 440
  • 4
  • 16
0

Do you have the old version of the library installed too? It's possible that gcc is looking for that version first, and doesn't consider the new one.

Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
  • Yes I have the old one as well but cannot remove it (some other things need it). This is why I though putting an explicit path and/or library name would load the new one first. – edA-qa mort-ora-y Mar 03 '14 at 05:26
  • And it does find it. If I put `-l:libyaml-cpp.so.0.5` is doesn't complain about a missing file, yet still doesn't find the references. – edA-qa mort-ora-y Mar 03 '14 at 05:27
0

I have been struggling over this for the whole evening today. As I found no useful info anywhere in the internet, I post my results here:

Using OSX El Capitan with new versions of XCode (7.3) and CLang (Apple LLVM version 7.3.0 (clang-703.0.29) Using yaml-cpp 0.5.3

Things I did to make it work:

  1. Obtain FindYamlCpp.cmake from the internet. Save it in /usr/local/share/cmake/Modules
  2. Set CMakeFiles.txt to find Yaml-cpp

    # YAML with yaml-cpp
    SET(YAMLCPP_STATIC_LIBRARY TRUE)
    FIND_PACKAGE(YamlCpp)
    IF(YamlCpp_FOUND)
        MESSAGE("yaml-cpp Library FOUND: yaml-cpp related sources will be built.")
    ELSEIF(YamlCpp_FOUND)
        MESSAGE("yaml-cpp Library NOT FOUND!")
    ENDIF(YamlCpp_FOUND)
    
  3. Add code to src/CmakeFiles.txt to use FindYamlCpp

        # Enable Yaml 
        IF(YAMLCPP_FOUND)
            ADD_EXECUTABLE(my_exec my_source.cpp)
        ENDIF(YAMLCPP_FOUND)
    
  4. Using for example ccmake:

    1. set CMAKE_MODULE_PATH to /usr/local/share/cmake/Modules
    2. Set CMAKE_EXEC_LINKER_FLAGS to -lyaml-cpp
Joan Solà
  • 123
  • 6
0

I have solved this problem, here is my method:
You may have multiple yaml-cpp libs on your system PATH, perhaps /usr/lib and /usr/local/lib, so the project may link the wrong one.
However, it's difficult to clean up the yaml-cpp libs on your system, so you need to relocate your yaml-cpp lib install path.
try:

clone the source code
cmake -DCMAKE_INSTALL_PREFIX='your_own_path' ..
In your CMakeLists:
find_path(yaml-cpp REQUIRED PATHS your_own_path)
target_include_directories(${PROJECT_NAME} PRIVATE ${YAML_CPP_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE ${YAML_CPP_LIBRARIES})

Hope it can help you, at least it did to me.