# Can not link \`yaml-cpp\` with \`libtorch\`

**URL:** https://discourse.cmake.org/t/can-not-link-yaml-cpp-with-libtorch/6601
**Category:** Usage
**Tags:** os:linux, gen:makefiles
**Created:** [October 5, 2022, 6:30am UTC](https://discourse.cmake.org/t/can-not-link-yaml-cpp-with-libtorch/6601 "2022-10-05T06:30:00Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Master\_Andreas](https://discourse.cmake.org/user_avatar/discourse.cmake.org/master_andreas/32/1339_2.png) [@Master\_Andreas](https://discourse.cmake.org/u/Master_Andreas)
#### Post date: [October 5, 2022, 6:30am UTC](https://discourse.cmake.org/t/can-not-link-yaml-cpp-with-libtorch/6601/1 "2022-10-05T06:30:00Z")

</div>

```auto
$ cmake --version                           
cmake version 3.24.2

```

CMakeList.txt

```nohighlight
cmake_minimum_required(VERSION 3.0 FATAL_ERROR) 
project(test_project)

set(Torch_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libtorch/share/cmake/Torch")
find_package(yaml-cpp REQUIRED)
find_package(Torch REQUIRED)

include_directories(
  ${yaml-cpp_INCLUDE_DIR}
  ${Torch_INCLUDE_DIRS}
  )

set( LIBS_TO_LINK 
		  ${TORCH_LIBRARIES}
		  yaml-cpp
		  )

add_executable(testWarehouse testWarehouse.cpp )
target_link_libraries( testWarehouse ${LIBS_TO_LINK} )

```

main file (testWarehouse.cpp in my case)

```nohighlight
#include <yaml-cpp/yaml.h>
//#include <torch/torch.h>

int main(int argc, char* argv[]){
	YAML::Node test_node = YAML::LoadFile("test");
}

```

output

```bash
cmake .. && make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/master-andreas/temp/build
Consolidate compiler generated dependencies of target testWarehouse
[50%] Building CXX object CMakeFiles/testWarehouse.dir/testWarehouse.cpp.o
[100%] Linking CXX executable testWarehouse
/usr/bin/ld: CMakeFiles/testWarehouse.dir/testWarehouse.cpp.o: in function `main':
testWarehouse.cpp:(.text+0x54): undefined reference to `YAML::LoadFile(std::string const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/testWarehouse.dir/build.make:102: testWarehouse] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/testWarehouse.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

```

Note: if you want to replicate, you will have to install `libtorch`

```auto
wget https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-1.10.1%2Bcpu.zip
unzip libtorch-shared-with-deps-1.10.1+cpu.zip
rm libtorch-shared-with-deps-1.10.1+cpu.zip

```

I have tried `libtorch` versions 10.1, 10.2, 11.0,12.0,12.1 and `yaml-cpp versions` 6.3, 7.0  
all with the same results

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [October 5, 2022, 7:02am UTC](https://discourse.cmake.org/t/can-not-link-yaml-cpp-with-libtorch/6601/2 "2022-10-05T07:02:37Z")

</div>

> [@Master\_Andreas](#):
>
> ```auto
> set( LIBS_TO_LINK 
> ${TORCH_LIBRARIES}
> yaml-cpp
> )
> 
> ```

Are you sure it’s `yaml-cpp` and not `YAML_CPP_LIBRARIES`?  
This is the docu comment from yaml-cpp config:

```auto
# - Config file for the yaml-cpp package
# It defines the following variables
# YAML_CPP_INCLUDE_DIR - include directory
# YAML_CPP_LIBRARY_DIR - directory containing libraries
# YAML_CPP_SHARED_LIBS_BUILT - whether we have built shared libraries or not
# YAML_CPP_LIBRARIES - libraries to link against

```

---

<div class="post-metadata">

### Author: ![Master\_Andreas](https://discourse.cmake.org/user_avatar/discourse.cmake.org/master_andreas/32/1339_2.png) [@Master\_Andreas](https://discourse.cmake.org/u/Master_Andreas)
#### Post date: [October 5, 2022, 7:19am UTC](https://discourse.cmake.org/t/can-not-link-yaml-cpp-with-libtorch/6601/3 "2022-10-05T07:19:11Z")

</div>

```auto
set( LIBS_TO_LINK #
		  ${TORCH_LIBRARIES}
		  #yaml-cpp
			${YAML_CPP_LIBRARIES}
		  )

```

This still fails

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [October 5, 2022, 7:29am UTC](https://discourse.cmake.org/t/can-not-link-yaml-cpp-with-libtorch/6601/4 "2022-10-05T07:29:18Z")

</div>

Well, you probably have to check what’s in those variables. Maybe those are only library names and one have to add `YAML_CPP_LIBRARY_DIR` to the link directories?

---

<div class="post-metadata">

### Author: ![Master\_Andreas](https://discourse.cmake.org/user_avatar/discourse.cmake.org/master_andreas/32/1339_2.png) [@Master\_Andreas](https://discourse.cmake.org/u/Master_Andreas)
#### Post date: [October 5, 2022, 7:41am UTC](https://discourse.cmake.org/t/can-not-link-yaml-cpp-with-libtorch/6601/5 "2022-10-05T07:41:42Z")

</div>

```auto
message("${YAML_CPP_LIBRARIES}")

```

```bash
/usr/lib/libyaml-cpp.so.0.7.0

```

This looks right to me

```auto
message("${YAML_CPP_LIBRARY_DIR}")

```

This appears to be empty, could it be an issue

---

<div class="post-metadata">

### Author: ![Master\_Andreas](https://discourse.cmake.org/user_avatar/discourse.cmake.org/master_andreas/32/1339_2.png) [@Master\_Andreas](https://discourse.cmake.org/u/Master_Andreas)
#### Post date: [October 7, 2022, 2:32pm UTC](https://discourse.cmake.org/t/can-not-link-yaml-cpp-with-libtorch/6601/7 "2022-10-07T14:32:02Z")

</div>

To clarify (because with @fenrir, the topic went a bitt off-topic), It can link just `yaml-cpp` it can link just `libtorch`, but when trying to link both it fails

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [October 7, 2022, 3:09pm UTC](https://discourse.cmake.org/t/can-not-link-yaml-cpp-with-libtorch/6601/8 "2022-10-07T15:09:22Z")

</div>

OK, I decided to google. In 2 Minutes I hit this: [Linking torch libraries and yaml-cpp gives undefined reference to yaml-cpp libraries · Issue #19353 · pytorch/pytorch · GitHub](https://github.com/pytorch/pytorch/issues/19353)  
so this is an ABI incompatibility. There’s a solution / workaround in second-to-last comment as I’m writing this.

TL;DR: Use `wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.12.1%2Bcpu.zip`
