0

我正在关注JetBrains在 Apple Silicon 计算机上的教程。我用 MacPorts (sudo port install boost) 安装了 boost,版本是 1.71。tests.cpp 文件的构建失败并出现以下错误:

ld: warning: ignoring file /opt/local/lib/libboost_unit_test_framework-mt.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
Undefined symbols for architecture x86_64:
  "boost::test_tools::tt_detail::report_assertion(boost::test_tools::assertion_result const&, boost::unit_test::lazy_ostream const&, boost::unit_test::basic_cstring<char const>, unsigned long, boost::test_tools::tt_detail::tool_level, boost::test_tools::tt_detail::check_type, unsigned long, ...)", referenced from:
      my_testsuite::addvalues_zero::test_method() in tests.cpp.o
      my_testsuite::addvalues_negpos::test_method() in tests.cpp.o
  "boost::test_tools::tt_detail::format_assertion_result(boost::unit_test::basic_cstring<char const>, boost::unit_test::basic_cstring<char const>)", referenced from:
      boost::test_tools::assertion::binary_expr<boost::test_tools::assertion::value_expr<int>, int, boost::test_tools::assertion::op::EQ<int, int, void> >::evaluate(bool) const in tests.cpp.o
  "boost::unit_test::lazy_ostream::inst", referenced from:
      boost::unit_test::lazy_ostream::instance() in tests.cpp.o
  "boost::unit_test::unit_test_main(bool (*)(), int, char**)", referenced from:
      _main in tests.cpp.o
  "boost::unit_test::unit_test_log_t::set_checkpoint(boost::unit_test::basic_cstring<char const>, unsigned long, boost::unit_test::basic_cstring<char const>)", referenced from:
      my_testsuite::addvalues_zero_invoker() in tests.cpp.o
      my_testsuite::addvalues_zero::test_method() in tests.cpp.o
      my_testsuite::addvalues_negpos_invoker() in tests.cpp.o
      my_testsuite::addvalues_negpos::test_method() in tests.cpp.o
  "boost::unit_test::unit_test_log_t::instance()", referenced from:
      ___cxx_global_var_init in tests.cpp.o
  "boost::unit_test::decorator::collector_t::instance()", referenced from:
      ___cxx_global_var_init.3 in tests.cpp.o
      ___cxx_global_var_init.6 in tests.cpp.o
      ___cxx_global_var_init.10 in tests.cpp.o
  "boost::unit_test::framework::impl::master_test_suite_name_setter::master_test_suite_name_setter(boost::unit_test::basic_cstring<char const>)", referenced from:
      ___cxx_global_var_init.1 in tests.cpp.o
  "boost::unit_test::test_case::test_case(boost::unit_test::basic_cstring<char const>, boost::unit_test::basic_cstring<char const>, unsigned long, boost::function<void ()> const&)", referenced from:
      boost::unit_test::make_test_case(boost::function<void ()> const&, boost::unit_test::basic_cstring<char const>, boost::unit_test::basic_cstring<char const>, unsigned long) in tests.cpp.o
  "boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(boost::unit_test::basic_cstring<char const>, boost::unit_test::basic_cstring<char const>, unsigned long, boost::unit_test::decorator::collector_t&)", referenced from:
      ___cxx_global_var_init.3 in tests.cpp.o
  "boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(boost::unit_test::test_case*, boost::unit_test::decorator::collector_t&, unsigned long)", referenced from:
      ___cxx_global_var_init.6 in tests.cpp.o
      ___cxx_global_var_init.10 in tests.cpp.o
  "boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(int)", referenced from:
      ___cxx_global_var_init.15 in tests.cpp.o
  "boost::unit_test::ut_detail::normalize_test_case_name(boost::unit_test::basic_cstring<char const>)", referenced from:
      boost::unit_test::make_test_case(boost::function<void ()> const&, boost::unit_test::basic_cstring<char const>, boost::unit_test::basic_cstring<char const>, unsigned long) in tests.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [test/test] Error 1
make[2]: *** [test/CMakeFiles/test.dir/all] Error 2
make[1]: *** [test/CMakeFiles/test.dir/rule] Error 2
make: *** [test] Error 2

我在tests.cpp中的代码:

#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE my_unit_tests
#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>
#include "../calc.h"

BOOST_AUTO_TEST_SUITE(my_testsuite)

    BOOST_AUTO_TEST_CASE(addvalues_zero)
    {
        BOOST_TEST(add(0, 0) == 0);
        BOOST_TEST(add(-5, 5) == 0);
    }

    BOOST_AUTO_TEST_CASE(addvalues_negpos)
    {
        BOOST_TEST(add(-5, 10) == 5);
        BOOST_TEST(add(-10, 5) == -5);
        BOOST_TEST(add(5, 10) == 15);
    }

BOOST_AUTO_TEST_SUITE_END()

我在 test/CMakeLists.txt 中的代码:

set(Boost_USE_STATIC_LIBS OFF)
find_package(Boost REQUIRED COMPONENTS unit_test_framework)

include_directories(${Boost_INCLUDE_DIR})

add_executable(test tests.cpp)
target_link_libraries(test ${Boost_LIBRARIES})
target_link_libraries(test test_lib)

我在 cmake-build-debug 文件夹中有库,并且在主 CMakeLists.txt 中包含了测试子项目。

4

2 回答 2

1

可能你也需要这个:

set(CMAKE_SYSTEM_PROCESSOR arm64)
set(CMAKE_OSX_ARCHITECTURES arm64)

顺便说一句,如果您遇到这些错误,可能是您使用了过时的 macOS 版本,或者您没有下载 XCode。

于 2021-05-30T18:14:30.910 回答
1

添加

set(CMAKE_OSX_ARCHITECTURES, arm64)

在根 CmakeLists.txt 的最开始。

于 2021-01-03T12:57:33.440 回答