Linking C Program with Boost Using CMake
This guide addresses the issue of linking a C program with the Boost library using CMake. When attempting to link the program, users may encounter errors such as "undefined reference to `boost::program_options::options_description::m_default_line_length'".
CMake Configuration for Linking
To resolve this issue, modify the CMake file to incorporate the following lines:
find_package(Boost 1.40 COMPONENTS program_options REQUIRED) include_directories(${Boost_INCLUDE_DIR}) add_executable(my_target_file main.cpp) target_link_libraries(my_target_file LINK_PUBLIC ${Boost_LIBRARIES})
Explanation of the Code
Alternative Approach
If the find_package method fails, you can manually specify the Boost library path and name, as seen below:
include_directories(/path/to/Boost/include) target_link_libraries(my_target_file ${Boost_PROGRAM_OPTIONS_LIBRARY})
Additional Resources
The above is the detailed content of How to Successfully Link a C Program with Boost Using CMake?. For more information, please follow other related articles on the PHP Chinese website!