关于编译 c++ 的错误使用 cmake 和 boost 编写代码

发布于 2025-01-04 01:38:02 字数 1854 浏览 1 评论 0原文

我正在尝试使用 CMake 编译依赖于 boost 的 c++ 软件。使用相同的源代码和 CMakeLists.txt 文件,我在自己的笔记本电脑(带有 boost1.42 的 ubuntu 11)上取得了成功,但是我在研究组(顺便说一句)中的工作站(带有 boost 1.41 的 RHEL6.2)收到以下错误消息,boost1.41应该足够了):

main/main.cpp: In function ‘path
make_path(const std::string&)’:
main/main.cpp:50: error: invalid
conversion from ‘bool (*)(const std::string&)’ to ‘void*’
main/main.cpp:50: error:
initializing argument 2 of ‘boost::filesystem3::path::path(const Source&,
typename
boost::enable_if<boost::filesystem3::path_traits::is_pathable<typename
boost::decay<Source>::type>, void>::type*) [with Source =
std::basic_string<char, std::char_traits<char>, std::allocator<char> >]’
main/main.cpp: In function ‘int
main(int, char**)’:
main/main.cpp:664: error: ‘class
path’ has no member named ‘native_file_string’
main/main.cpp:676: error: ‘class
path’ has no member named ‘native_file_string’
make[2]: *** [main/CMakeFiles/vina_main.dir/main.cpp.o] Error 1
make[1]: *** [main/CMakeFiles/vina_main.dir/all] Error 2
make: *** [all] Error 2

我不太明白错误消息,也不知道如何修复它。谁能帮助我吗?

=======================更新

==========================感谢您的帮助,上述错误消息已得到修复,但我仍然收到错误消息,指示我的可执行文件和 boost 库之间的链接失败。我确实通过“target_link_libraries (vvv_main vvv ${Boost_LIBRARIES})”在 CMakeLists.txt 中链接了它。错误消息如下:

CMakeFiles/vvv_main.dir/main.cpp.o: In function              '__static_initialization_and_destruction_0':
/usr/local/include/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'

......

我在这里阅读了其他相关帖子,但仍然不知道如何解决我的问题。谢谢!

I am trying to compile c++ software depending on boost with CMake. With the same source code and CMakeLists.txt files, I succeeded with my own laptop (ubuntu 11 with boost1.42), but I am getting the following error message with my workstation (RHEL6.2 with boost 1.41) in research group (btw, boost1.41 should be enough):

main/main.cpp: In function ‘path
make_path(const std::string&)’:
main/main.cpp:50: error: invalid
conversion from ‘bool (*)(const std::string&)’ to ‘void*’
main/main.cpp:50: error:
initializing argument 2 of ‘boost::filesystem3::path::path(const Source&,
typename
boost::enable_if<boost::filesystem3::path_traits::is_pathable<typename
boost::decay<Source>::type>, void>::type*) [with Source =
std::basic_string<char, std::char_traits<char>, std::allocator<char> >]’
main/main.cpp: In function ‘int
main(int, char**)’:
main/main.cpp:664: error: ‘class
path’ has no member named ‘native_file_string’
main/main.cpp:676: error: ‘class
path’ has no member named ‘native_file_string’
make[2]: *** [main/CMakeFiles/vina_main.dir/main.cpp.o] Error 1
make[1]: *** [main/CMakeFiles/vina_main.dir/all] Error 2
make: *** [all] Error 2

I don't quite understand the error message and don't know how to fix it. Can anyone help me?

======================= update =========================

The above error message is fixed thanks to your help, but I still get the error message indicating linking failure between my executable file and boost libraries. I did link it within CMakeLists.txt by 'target_link_libraries (vvv_main vvv ${Boost_LIBRARIES})'. The error message is like:

CMakeFiles/vvv_main.dir/main.cpp.o: In function              '__static_initialization_and_destruction_0':
/usr/local/include/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'

.......

I read other related posts here, but still have no clue how to fix my problem. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭白衣梦中忆 2025-01-11 01:38:02

我建议您将 CMake 的 find_package 例程与 required 标志一起使用,并显式指定您需要的组件。这样就不需要手动设置库了。

对于您的项目

FIND_PACKAGE(Boost 1.41 COMPONENTS filesystem system REQUIRED)
MESSAGE(STATUS "** Boost Include: ${Boost_INCLUDE_DIR}")
MESSAGE(STATUS "** Boost Libraries: ${Boost_LIBRARIES}")

来说,

TARGET_LINK_LIBRARIES(your_project ${Boost_LIBRARIES})

应该可以解决问题。如果只有旧版本的 boost 可用,指定版本(此处为 1.41)将使 CMake 生效。指定组件(组件 = 不仅仅是头文件的 boost 库)会使 CMake 抱怨这些组件不可用,并且还会自动将它们添加到变量 Boost_LIBRARIES 中。消息语句不是必需的,但我想得到一些反馈...

如果您想手动下载并安装最新版本的 boost(我通常这样做,因为 Linux 发行版在 boost 打包方面往往有点慢)与系统提供的版本一起,然后您可以提示 find_package 脚本以将 boost 提升到自定义位置,

SET(BOOST_ROOT "$ENV{HOME}/usr") 

当然,在 find_package 调用之前(在本例中 boost 将被安装到前缀 $主目录/usr)。

最后一点 - 我有时会遇到旧版本的 FindBoost.cmake 脚本(尤其是旧版 CMake 2.6 中的脚本)的问题,该脚本在 find_package(Boost) 时发挥了所有作用。代码 > 被调用。对于 Boost 1.41 来说这应该不是问题,但如果在查找(较新版本的)Boost 时出现问题,即使您完全确定它已正确安装,您可能需要尝试更新版本的 FindBoost。 .cmake.例如,来自 gitweb ,它应该位于 Modules 中树。

I would suggest, that you use the find_package routines of CMake together with the required flag and explicitly specifying the components you need. Then there is no need to manually set the libraries.

For your project

FIND_PACKAGE(Boost 1.41 COMPONENTS filesystem system REQUIRED)
MESSAGE(STATUS "** Boost Include: ${Boost_INCLUDE_DIR}")
MESSAGE(STATUS "** Boost Libraries: ${Boost_LIBRARIES}")

together with

TARGET_LINK_LIBRARIES(your_project ${Boost_LIBRARIES})

should do the trick. Specifying the version (here 1.41) makes CMake if only an older version of boost is available. Specifying the components (components = boost libraries that are not header-only) makes CMake complain if those are not available and also automatically adds those to the variable Boost_LIBRARIES. The Message statements are not necessary but I like to have some feedback...

In case you would like to manually download and install the newest version of boost (which I normally do since Linux distributions tend to be somewhat slow with respect to boost packaging) alongside the version provided by the system, then you can hint the find_package scripts for boost to the custom location via

SET(BOOST_ROOT "$ENV{HOME}/usr") 

which of course goes before the find_package call (in this example boost would have been installed into the prefix $HOME/usr).

One last remark - I sometimes used to have problems with older versions of the FindBoost.cmake script (especially those from the old CMake 2.6) that does all the magic when find_package(Boost) is called. It should not be a problem for Boost 1.41, but in case there are problems finding (newer versions of) Boost even though you're absolutely sure it is correctly installed, you might want to try a more recent version of FindBoost.cmake. For example from the gitweb, it should be in the Modules tree.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文