Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

c++ - Can't build test suite in cmake project with Boost.Test on Apple Silicon

I'm following JetBrains's tutorial on an Apple Silicon computer. I installed boost with MacPorts (sudo port install boost), the version is 1.71 The build of tests.cpp file fails with the following error:

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

My code in 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()

My code in 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)

I have the library in the cmake-build-debug folder and I have included the test subproject in the main CMakeLists.txt.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add

set(CMAKE_OSX_ARCHITECTURES, arm64)

in the most beginning of the root CmakeLists.txt.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...