I’m encountering linker errors when trying to use Boost::filesystem with Conan package manager and CMake. I’ve successfully installed Boost via Conan, but I’m unable to link against Boost::filesystem. Here are the details:
This is my project/conanfile.txt
:
[requires]
boost/1.86.0
[generators]
CMakeDeps
CMakeToolchain
To install dependencies, I navigate to the root directory (cd project
) and run:
conan install . -of=external --build=MISSING
which outputs:
======== Finalizing install (deploy, generators) ========
conanfile.txt: Writing generators to <redacted>/TestProject/external
conanfile.txt: Generator 'CMakeDeps' calling 'generate()'
conanfile.txt: CMakeDeps necessary find_package() and targets for your CMakeLists.txt
find_package(Boost)
target_link_libraries(... boost::boost)
conanfile.txt: Generator 'CMakeToolchain' calling 'generate()'
conanfile.txt: CMakeToolchain generated: conan_toolchain.cmake
conanfile.txt: CMakeToolchain: Preset 'conan-release' added to CMakePresets.json.
(cmake>=3.23) cmake --preset conan-release
(cmake<3.23) cmake <path> -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_BUILD_TYPE=Release
conanfile.txt: CMakeToolchain generated: <redacted>/TestProject/external/CMakePresets.json
conanfile.txt: CMakeToolchain generated: <redacted>/TestProject/CMakeUserPresets.json
conanfile.txt: Generating aggregated env files
conanfile.txt: Generated aggregated env files: ['conanbuild.sh', 'conanrun.sh']
Install finished successfully
This successfully installs all dependencies (and transitive dependencies). The issue I’m having relates to linking the libraries from Boost. In my project/CMakeLists.txt
cmake_minimum_required(VERSION 3.29)
project(TestProject)
add_executable(TestProject main.cpp)
find_package(Boost)
set(Boost_FILESYSTEM_LIB_PATH ${Boost_INCLUDE_DIRS}/../lib/libboost_filesystem.a)
target_include_directories(TestProject PUBLIC ${Boost_INCLUDE_DIRS})
target_link_libraries(TestProject
PUBLIC
# This works
Boost::boost
# This works (When commented out, linker error below occurs)
#${Boost_FILESYSTEM_LIB_PATH}
# Using this to link introduces linker errors
Boost::filesystem
)
Here is the code which attempts to use filesystem symbols project/main.cpp
:
#include <iostream>
#include <boost/filesystem.hpp>
int main()
{
auto ret = boost::filesystem::exists("/foo");
std::cout << ret << std::endl;
return 0;
}
I now run the program. The general structure of the project is as follows:
project/CMakeLists.txt
project/external (Conan output folder)
project/main.cpp
project/build
To build (via terminal) using Makefile:
cd project/build
cmake -DCMAKE_TOOLCHAIN_FILE=external/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Debug ..
make
Here is the linker error output when attempting to link via Boost::filesystem
:
[ 50%] Building CXX object CMakeFiles/TestProject.dir/main.cpp.o
[100%] Linking CXX executable TestProject
Undefined symbols for architecture arm64:
"boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
boost::filesystem::exists(boost::filesystem::path const&) in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Here is the output when attempting to link via ${Boost_FILESYSTEM_LIB_PATH
:
[ 50%] Building CXX object CMakeFiles/TestProject.dir/main.cpp.o
[100%] Linking CXX executable TestProject
[100%] Built target TestProject
I have attempted to change the line:
find_package(Boost)
to: find_package(Boost REQUIRED COMPONENTS filesystem)
to no avail.
How can I resolve these linker errors (instead of using explicit library pathing)? I have read the Conan tutorials and watched a few videos. I have encountered it with several libraries. I am still learning how to use Conan, so I may be missing something painfully obvious. Thank you for any assistance.
7
You can’t change settings like CMAKE_BUILD_TYPE
on the cmake command line when using Conan, you’ve installed release packages with conan install
but when you change the build type to debug, the Conan targets will be generated to link to the non-existant debug versions of the libraries (you don’t get an error as Conan ends up using undefined variables which cmake just ignores).
You should either setup a separate Conan profile for debug builds or set the debug setting during Conan install:
conan install . -of=external --build=MISSING -s build_type=debug
See https://docs.conan.io/2/tutorial/consuming_packages/different_configurations.html
If you want to still use the release packages then you can use:
conan install . -of=external --build=MISSING -s &:build_type=Debug -s build_type=Release
You’ll need to make sure that the debug and release packages have the same ABI before doing this (they don’t with MSVC).
1