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'
This error arises when the necessary Boost library is not properly linked to the program.
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})
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!