# Boost Linker Issues

**URL:** https://discourse.cmake.org/t/boost-linker-issues/2030
**Category:** Code
**Created:** [October 20, 2020, 5:23am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030 "2020-10-20T05:23:43Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![LavaMaster](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lavamaster/32/890_2.png) [@LavaMaster](https://discourse.cmake.org/u/LavaMaster)
#### Post date: [October 20, 2020, 5:23am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/1 "2020-10-20T05:23:43Z")

</div>

I am using the Boost library and I am having some linker issues. Currently my code is outputting this:

> Undefined symbols for architecture x86\_64: “boost::program\_options::to\_internal(std::\_\_1::basic\_string\<char, std::\_\_1::char\_traits, std::\_\_1::allocator \> const&)”, referenced from: std::\_\_1::vector\<std::\_\_1::basic\_string\<char, std::\_\_1::char\_traits, std::\_\_1::allocator \>, std::\_\_1::allocator\<std::\_\_1::basic\_string\<char, std::\_\_1::char\_traits, std::\_\_1::allocator \> \> \> boost::program\_options::to\_internal\<std::\_\_1::basic\_string\<char, std::\_\_1::char\_traits, std::\_\_1::allocator \> \>(std::\_\_1::vector\<std::\_\_1::basic\_string\<char, std::\_\_1::char\_traits, std::\_\_1::allocator \>, std::\_\_1::allocator\<std::\_\_1::basic\_string\<char, std::\_\_1::char\_traits, std::\_\_1::allocator \> \> \> const&) in train\_model\_main.cc.o “boost::program\_options::variables\_map::variables\_map()”, referenced from: \_main in train\_model\_main.cc.o (THE LIST CONTINUES)

At the bottom my code says this:

> ld: symbol(s) not found for architecture x86\_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am trying to use the program\_options library from Boost, but the linking step seems to be failing. Here is how I link in my CMake file:

```auto
if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(main ./apps/something.cc)
    target_link_libraries( main program_options)
endif()

```

I believe that I am correctly linking the library, so what could be causing this issue?

---

<div class="post-metadata">

### Author: ![vre](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vre](https://discourse.cmake.org/u/vre)
#### Post date: [October 20, 2020, 6:47am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/2 "2020-10-20T06:47:05Z")

</div>

_“I believe that I am correctly linking the library”_ unfortunately not. You need to reference the full target name including the namespace prefix with `Boost::program_options`. And you can omit adding the Boost\_INCLUDE\_DIRS explicitely as the target definition does include this information. So your lines would look like:

```
if(Boost_FOUND)
    add_executable(main ./apps/something.cc)
    target_link_libraries(main PUBLIC Boost::program_options)
endif()
```

---

<div class="post-metadata">

### Author: ![LavaMaster](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lavamaster/32/890_2.png) [@LavaMaster](https://discourse.cmake.org/u/LavaMaster)
#### Post date: [October 20, 2020, 6:54am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/3 "2020-10-20T06:54:52Z")

</div>

This seemed to fix the linker issue, however, I am getting this issue now:

> fatal error: ‘boost/program\_options.hpp’ file not found  
> #include \<boost/program\_options.hpp\>

---

<div class="post-metadata">

### Author: ![vre](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vre](https://discourse.cmake.org/u/vre)
#### Post date: [October 20, 2020, 7:06am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/4 "2020-10-20T07:06:06Z")

</div>

What platform are you on, what version of Boost are you trying to link to and how does your find\_package command for Boost does look like (including all settings for Boost)?

A typical command sequence for Boost in my CMakeLists.txt looks like this:

```
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost REQUIRED program_options)
```

---

<div class="post-metadata">

### Author: ![LavaMaster](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lavamaster/32/890_2.png) [@LavaMaster](https://discourse.cmake.org/u/LavaMaster)
#### Post date: [October 20, 2020, 7:09am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/5 "2020-10-20T07:09:14Z")

</div>

I am on MacOS. I am using Boost Version 1.73.0. My find\_package:  
find\_package(Boost 1.73.0 COMPONENTS program\_options REQUIRED)  
After making the changes you suggested, I received the “fatal error: ‘boost/program\_options.hpp’ file not found” which occurred once 87% of the build was complete. After adding " ` INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})`" outside of the endif() the build completes but then I get the same issue as before (Undefined symbols for architecture x86\_64:, etc and at the bottom it says: NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.  
ld: symbol(s) not found for architecture x86\_64  
clang: error: linker command failed with exit code 1 (use -v to see invocation))

---

<div class="post-metadata">

### Author: ![LavaMaster](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lavamaster/32/890_2.png) [@LavaMaster](https://discourse.cmake.org/u/LavaMaster)
#### Post date: [October 20, 2020, 7:12am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/6 "2020-10-20T07:12:01Z")

</div>

I believe the step my code is failing on (when building) is:

“Linking CXX executable train-model”

---

<div class="post-metadata">

### Author: ![vre](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vre](https://discourse.cmake.org/u/vre)
#### Post date: [October 20, 2020, 7:16am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/7 "2020-10-20T07:16:08Z")

</div>

Does this target differs from the one you built with `add_executable(main ./apps/something.cc)`  
or is it the same?

---

<div class="post-metadata">

### Author: ![LavaMaster](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lavamaster/32/890_2.png) [@LavaMaster](https://discourse.cmake.org/u/LavaMaster)
#### Post date: [October 20, 2020, 7:17am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/8 "2020-10-20T07:17:13Z")

</div>

When you say “target”, what are you referring to?

---

<div class="post-metadata">

### Author: ![vre](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vre](https://discourse.cmake.org/u/vre)
#### Post date: [October 20, 2020, 7:18am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/9 "2020-10-20T07:18:15Z")

</div>

The `main` in `add_executable` command. `add_executable` adds a target to the CMakeLists.txt.

---

<div class="post-metadata">

### Author: ![LavaMaster](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lavamaster/32/890_2.png) [@LavaMaster](https://discourse.cmake.org/u/LavaMaster)
#### Post date: [October 20, 2020, 7:20am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/10 "2020-10-20T07:20:05Z")

</div>

The target I built with add\_executable is the file I am running from (the main method). I get these errors when running something.cc. Also, “Linking CXX executable train-model” train-model is something.cc.

---

<div class="post-metadata">

### Author: ![vre](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vre](https://discourse.cmake.org/u/vre)
#### Post date: [October 20, 2020, 7:27am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/11 "2020-10-20T07:27:52Z")

</div>

Did you built Boost by yourself or installed it via Homebrew or Macports?

---

<div class="post-metadata">

### Author: ![LavaMaster](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lavamaster/32/890_2.png) [@LavaMaster](https://discourse.cmake.org/u/LavaMaster)
#### Post date: [October 20, 2020, 7:28am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/12 "2020-10-20T07:28:50Z")

</div>

I installed it via Homebrew. However, I initially installed it from the website (the .tar file). I installed it via homebrew without uninstalling the .tar files. After installing it via homebrew, I deleted the .tar files.

---

<div class="post-metadata">

### Author: ![vre](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vre](https://discourse.cmake.org/u/vre)
#### Post date: [October 20, 2020, 7:31am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/13 "2020-10-20T07:31:08Z")

</div>

Did you clear the CMakeCache.txt or the entire build directory after applying those changes? Otherwise some settings might still be cached by CMake.

---

<div class="post-metadata">

### Author: ![LavaMaster](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lavamaster/32/890_2.png) [@LavaMaster](https://discourse.cmake.org/u/LavaMaster)
#### Post date: [October 20, 2020, 7:31am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/14 "2020-10-20T07:31:56Z")

</div>

I did not, how would I go about do that? And should I do that right now or is it too late?

---

<div class="post-metadata">

### Author: ![vre](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vre](https://discourse.cmake.org/u/vre)
#### Post date: [October 20, 2020, 7:33am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/15 "2020-10-20T07:33:37Z")

</div>

It’s never too late :). Delete the CMakeCache.txt from the build directory and rerun the CMake configure command.

---

<div class="post-metadata">

### Author: ![LavaMaster](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lavamaster/32/890_2.png) [@LavaMaster](https://discourse.cmake.org/u/LavaMaster)
#### Post date: [October 20, 2020, 7:36am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/16 "2020-10-20T07:36:09Z")

</div>

I deleted it. What exactly do I need to rerun to make it reappear? Sorry, I am new to this stuff.

---

<div class="post-metadata">

### Author: ![vre](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vre](https://discourse.cmake.org/u/vre)
#### Post date: [October 20, 2020, 7:39am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/17 "2020-10-20T07:39:29Z")

</div>

Just call your configure command. A typical sequence from a build dir under the source dir is:

```
mkdir build
cd build
cmake ..
make
make install

```

I mean the command that configures your project (`cmake ..`)

---

<div class="post-metadata">

### Author: ![LavaMaster](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lavamaster/32/890_2.png) [@LavaMaster](https://discourse.cmake.org/u/LavaMaster)
#### Post date: [October 20, 2020, 7:45am UTC](https://discourse.cmake.org/t/boost-linker-issues/2030/18 "2020-10-20T07:45:05Z")

</div>

Reconfigured it, still getting the same issue.
