Home > Backend Development > C++ > How to Link C Programs with Boost Using CMake on Ubuntu?

How to Link C Programs with Boost Using CMake on Ubuntu?

Mary-Kate Olsen
Release: 2024-12-03 10:08:22
Original
824 people have browsed it

How to Link C   Programs with Boost Using CMake on Ubuntu?

Linking C programs with Boost using CMake on Ubuntu

Issue

When attempting to link a C program with the Boost library on Ubuntu, the following error may be encountered:

main.cpp:(.text+0x3b): undefined reference to `boost::program_options::options_description::m_default_line_length'
Copy after login

This error arises when the necessary Boost library is not properly linked to the program.

Solution using CMake

To resolve this issue, it is essential to leverage CMake's find_package function to locate the Boost library. Typically, a script named FindBoost.cmake is included with most CMake installations.

This script will provide instructions on how to use Boost_INCLUDE_DIR to include Boost header files and Boost_LIBRARIES to link to Boost libraries in your CMake project. Here's an example of how to achieve this:

FIND_PACKAGE(Boost 1.40 COMPONENTS program_options REQUIRED)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})

ADD_EXECUTABLE(anyExecutable myMain.cpp)

TARGET_LINK_LIBRARIES(anyExecutable LINK_PUBLIC ${Boost_LIBRARIES})
Copy after login

Additional Information

  • For further information regarding FindBoost.cmake, refer to the official Boost documentation: [CMake docs](https://cmake.org/Wiki/FindBoost)
  • To access the source code for FindBoost.cmake, visit the GitHub repository: [FindBoost.cmake](https://github.com/Kitware/CMake/blob/master/Modules/FindBoost.cmake)

The above is the detailed content of How to Link C Programs with Boost Using CMake on Ubuntu?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template