0

私は C++、Boost テスト、および CMake にかなり慣れていないため、ここでのつまずきをご容赦ください。

これが私のトップレベルの CMakeLists.txt です

cmake_minimum_required(VERSION 2.8)
project(JANKEN)

set ( CMAKE_CXX_COMPILER gcc )
set(CMAKE_CXX_FLAGS "-g -std=c++11 -Wall -Wextra -pedantic -Weffc++")

add_subdirectory(test)
enable_testing()

そして、これがテストディレクトリの横にあるCMakeLists.txtで、TestRunner.cppとtypes_test.cppが含まれています

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON) 
set(Boost_USE_STATIC_RUNTIME OFF)

set(BOOST_LIBS unit_test_framework system)
find_package( Boost REQUIRED COMPONENTS ${BOOST_LIBS})

include_directories( ${Boost_INCLUDE_DIRS}
)

add_executable(
JANKEN_tests
 TestRunner.cpp
 types_test.cpp
)

target_link_libraries(
JANKEN_tests 
${Boost_LIBRARIES}
boost_system-mt
)

TestRunner.cpp は次のようになります

#define BOOST_TEST_DYN_LINK

#define BOOST_TEST_MODULE "JANKEN Unit Tests"
#include <boost/test/unit_test.hpp>

結果のメイクファイルを実行すると、

piyos-imac:test piyo$ make
-- Boost version: 1.52.0
-- Found the following Boost libraries:
--   unit_test_framework
--   system
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/piyo/Source/janken/build
Linking CXX executable JANKEN_tests
ld: library not found for -lboost_system-mt
collect2: error: ld returned 1 exit status
make[2]: *** [test/types/JANKEN_tests] Error 1
make[1]: *** [test/types/CMakeFiles/JANKEN_tests.dir/all] Error 2
make: *** [all] Error 2

検索した後、 add_executable 行の前に次の行を追加してみました。

#link_directories ( ${Boost_LIBRARY_DIRS} )

ただし、これを追加すると、巨大な「アーキテクチャ x86_64 の未定義シンボル」エラーが発生し、文字列、ostream、vtables の欠落などについて不平を言います。

最初はシステム ライブラリが必要なようには見えませんでしたが、それを完全に除外すると、巨大な undefined-symbols エラーが発生します。

誰かが私が間違っていることを知っていますか? 私は gcc 4.7 で Mountain Lion を使用しています。

助けてくれてありがとう。

Kleist の提案を試すために編集します。

なので、これに変更すると

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON) 
set(Boost_USE_STATIC_RUNTIME OFF)

set(BOOST_LIBS unit_test_framework system)
find_package( Boost REQUIRED COMPONENTS ${BOOST_LIBS})

include_directories( ${Boost_INCLUDE_DIRS}
)

link_directories ( ${Boost_LIBRARY_DIRS} )

# Create the unit tests executable
add_executable(
JANKEN_tests
 TestRunner.cpp
 types_test.cpp
)

# Link the libraries
target_link_libraries(
JANKEN_tests 
${Boost_LIBRARIES}
)

私はこの結果を得る

piyos-imac:test piyo$ make
-- Boost version: 1.52.0
-- Found the following Boost libraries:
--   unit_test_framework
--   system
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/piyo/Source/janken/build
Linking CXX executable JANKEN_tests
Undefined symbols for architecture x86_64:
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const", referenced from:
      boost::unit_test::basic_cstring<char const>::basic_cstring(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in types_test.cpp.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const", referenced from:
      boost::unit_test::basic_cstring<char const>::basic_cstring(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in types_test.cpp.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from:
      boost::test_tools::print_log_value<int>::operator()(std::basic_ostream<char, std::char_traits<char> >&, int const&) in types_test.cpp.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::assign(char const*, unsigned long)", referenced from:
      void boost::unit_test::assign_op<char, char const>(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::unit_test::basic_cstring<char const>, int) in TestRunner.cpp.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
      boost::unit_test::make_test_case(boost::unit_test::callback0<boost::unit_test::ut_detail::unused> const&, boost::unit_test::basic_cstring<char const>) in types_test.cpp.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in TestRunner.cpp.o
      __static_initialization_and_destruction_0(int, int) in types_test.cpp.o
  "std::ios_base::Init::~Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in TestRunner.cpp.o
      __static_initialization_and_destruction_0(int, int) in types_test.cpp.o
  "std::__throw_out_of_range(char const*)", referenced from:
      std::array<SideVal, 2ul>::at(unsigned long) const in types_test.cpp.o
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
      operator<<(std::basic_ostream<char, std::char_traits<char> >&, side_t) in types_test.cpp.o
      operator<<(std::basic_ostream<char, std::char_traits<char> >&, SideVal&) in types_test.cpp.o
      boost::unit_test::lazy_ostream_impl<char const (&) [38]>::operator()(std::basic_ostream<char, std::char_traits<char> >&) const in types_test.cpp.o
      boost::unit_test::lazy_ostream_impl<char const (&) [40]>::operator()(std::basic_ostream<char, std::char_traits<char> >&) const in types_test.cpp.o
      boost::unit_test::lazy_ostream_impl<char const (&) [1]>::operator()(std::basic_ostream<char, std::char_traits<char> >&) const in types_test.cpp.o
  "vtable for __cxxabiv1::__class_type_info", referenced from:
      typeinfo for boost::unit_test::test_observer in TestRunner.cpp.o
      typeinfo for boost::unit_test::ut_detail::callback0_impl<boost::unit_test::ut_detail::unused> in types_test.cpp.o
      typeinfo for boost::unit_test::lazy_ostream in types_test.cpp.o
      typeinfo for boost::unit_test::test_observer in types_test.cpp.o
      typeinfo for boost::detail::sp_counted_base in types_test.cpp.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "vtable for __cxxabiv1::__si_class_type_info", referenced from:
      typeinfo for boost::detail::sp_counted_impl_p<boost::unit_test::ut_detail::callback0_impl_t<boost::unit_test::ut_detail::unused, void (*)()> > in types_test.cpp.o
      typeinfo for boost::unit_test::lazy_ostream_impl<boost::test_tools::tt_detail::print_helper_t<side_t> const&> in types_test.cpp.o
      typeinfo for boost::unit_test::lazy_ostream_impl<boost::test_tools::tt_detail::print_helper_t<int> const&> in types_test.cpp.o
      typeinfo for boost::unit_test::ut_detail::callback0_impl_t<boost::unit_test::ut_detail::unused, void (*)()> in types_test.cpp.o
      typeinfo for boost::unit_test::lazy_ostream_impl<char const (&) [38]> in types_test.cpp.o
      typeinfo for boost::unit_test::lazy_ostream_impl<char const (&) [40]> in types_test.cpp.o
      typeinfo for boost::unit_test::lazy_ostream_impl<char const (&) [1]> in types_test.cpp.o
      ...
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
  "operator delete(void*)", referenced from:
      boost::unit_test::test_observer::~test_observer() in TestRunner.cpp.o
      boost::unit_test::test_observer::~test_observer() in TestRunner.cpp.o
      boost::unit_test::test_observer::~test_observer() in TestRunner.cpp.o
      boost::unit_test::unit_test_log_t::~unit_test_log_t() in TestRunner.cpp.o
      boost::detail::sp_counted_base::~sp_counted_base() in types_test.cpp.o
      boost::detail::sp_counted_base::~sp_counted_base() in types_test.cpp.o
      boost::detail::sp_counted_base::~sp_counted_base() in types_test.cpp.o
      ...
  "operator new(unsigned long)", referenced from:
      boost::unit_test::make_test_case(boost::unit_test::callback0<boost::unit_test::ut_detail::unused> const&, boost::unit_test::basic_cstring<char const>) in types_test.cpp.o
      boost::unit_test::callback0<boost::unit_test::ut_detail::unused>::callback0<void (*)()>(void (*)()) in types_test.cpp.o
      boost::detail::shared_count::shared_count<boost::unit_test::ut_detail::callback0_impl_t<boost::unit_test::ut_detail::unused, void (*)()> >(boost::unit_test::ut_detail::callback0_impl_t<boost::unit_test::ut_detail::unused, void (*)()>*) in types_test.cpp.o
  "___cxa_begin_catch", referenced from:
      typestest::testside::test_method()     in types_test.cpp.o
      boost::detail::shared_count::shared_count<boost::unit_test::ut_detail::callback0_impl_t<boost::unit_test::ut_detail::unused, void (*)()> >(boost::unit_test::ut_detail::callback0_impl_t<boost::unit_test::ut_detail::unused, void (*)()>*) in types_test.cpp.o
  "___cxa_end_catch", referenced from:
      typestest::testside::test_method()     in types_test.cpp.o
      boost::detail::shared_count::shared_count<boost::unit_test::ut_detail::callback0_impl_t<boost::unit_test::ut_detail::unused, void (*)()> >(boost::unit_test::ut_detail::callback0_impl_t<boost::unit_test::ut_detail::unused, void (*)()>*) in types_test.cpp.o
  "___cxa_guard_acquire", referenced from:
      boost::unit_test::singleton<boost::unit_test::unit_test_log_t>::instance() in TestRunner.cpp.o
      boost::unit_test::lazy_ostream::instance()    in types_test.cpp.o
      boost::unit_test::singleton<boost::unit_test::unit_test_log_t>::instance() in types_test.cpp.o
      boost::unit_test::ut_detail::auto_tc_exp_fail<typestest::testside_id>::instance()      in types_test.cpp.o
  "___cxa_guard_release", referenced from:
      boost::unit_test::singleton<boost::unit_test::unit_test_log_t>::instance() in TestRunner.cpp.o
      boost::unit_test::lazy_ostream::instance()    in types_test.cpp.o
      boost::unit_test::singleton<boost::unit_test::unit_test_log_t>::instance() in types_test.cpp.o
      boost::unit_test::ut_detail::auto_tc_exp_fail<typestest::testside_id>::instance()      in types_test.cpp.o
  "___cxa_pure_virtual", referenced from:
      vtable for boost::unit_test::ut_detail::callback0_impl<boost::unit_test::ut_detail::unused> in types_test.cpp.o
      vtable for boost::detail::sp_counted_base in types_test.cpp.o
  "___cxa_rethrow", referenced from:
      boost::detail::shared_count::shared_count<boost::unit_test::ut_detail::callback0_impl_t<boost::unit_test::ut_detail::unused, void (*)()> >(boost::unit_test::ut_detail::callback0_impl_t<boost::unit_test::ut_detail::unused, void (*)()>*) in types_test.cpp.o
  "___gxx_personality_v0", referenced from:
      Dwarf Exception Unwind Info (__eh_frame) in TestRunner.cpp.o
      Dwarf Exception Unwind Info (__eh_frame) in types_test.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [test/types/JANKEN_tests] Error 1
make[1]: *** [test/types/CMakeFiles/JANKEN_tests.dir/all] Error 2
make: *** [all] Error 2

これは、link_directories 行を含めるかどうかに関係なく発生します。

編集: make VERBOSE=1 結果を含む

piyos-imac:test piyo$ make VERBOSE=1
cd /Users/piyo/Source/janken/build && "/Applications/CMake 2.8-10.app/Contents/bin/cmake" -H/Users/piyo/Source/janken -B/Users/piyo/Source/janken/build --check-build-system CMakeFiles/Makefile.cmake 0
cd /Users/piyo/Source/janken/build && "/Applications/CMake 2.8-10.app/Contents/bin/cmake" -E cmake_progress_start /Users/piyo/Source/janken/build/CMakeFiles /Users/piyo/Source/janken/build/test/CMakeFiles/progress.marks
cd /Users/piyo/Source/janken/build && make -f CMakeFiles/Makefile2 test/all
make -f test/types/CMakeFiles/JANKEN_tests.dir/build.make test/types/CMakeFiles/JANKEN_tests.dir/depend
cd /Users/piyo/Source/janken/build && "/Applications/CMake 2.8-10.app/Contents/bin/cmake" -E cmake_depends "Unix Makefiles" /Users/piyo/Source/janken /Users/piyo/Source/janken/test/types /Users/piyo/Source/janken/build /Users/piyo/Source/janken/build/test/types /Users/piyo/Source/janken/build/test/types/CMakeFiles/JANKEN_tests.dir/DependInfo.cmake --color=
Dependee "/Users/piyo/Source/janken/test/types/types_test.cpp" is newer than depends file "/Users/piyo/Source/janken/build/test/types/CMakeFiles/JANKEN_tests.dir/depend.internal".
Clearing dependencies in "/Users/piyo/Source/janken/build/test/types/CMakeFiles/JANKEN_tests.dir/depend.make".
Scanning dependencies of target JANKEN_tests
make -f test/types/CMakeFiles/JANKEN_tests.dir/build.make test/types/CMakeFiles/JANKEN_tests.dir/build
"/Applications/CMake 2.8-10.app/Contents/bin/cmake" -E cmake_progress_report /Users/piyo/Source/janken/build/CMakeFiles 2
[ 50%] Building CXX object test/types/CMakeFiles/JANKEN_tests.dir/types_test.cpp.o
cd /Users/piyo/Source/janken/build/test/types && gcc    -g -std=c++11 -Wall -Wextra -pedantic -Weffc++   -o CMakeFiles/JANKEN_tests.dir/types_test.cpp.o -c /Users/piyo/Source/janken/test/types/types_test.cpp
/Users/piyo/Source/janken/test/types/types_test.cpp:12:1: warning: base class 'struct boost::unit_test::ut_detail::nil_t' has a non-virtual destructor [-Weffc++]
Linking CXX executable JANKEN_tests
cd /Users/piyo/Source/janken/build/test/types && "/Applications/CMake 2.8-10.app/Contents/bin/cmake" -E cmake_link_script CMakeFiles/JANKEN_tests.dir/link.txt --verbose=1
gcc   -g -std=c++11 -Wall -Wextra -pedantic -Weffc++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names   CMakeFiles/JANKEN_tests.dir/TestRunner.cpp.o CMakeFiles/JANKEN_tests.dir/types_test.cpp.o  -o JANKEN_tests  -L/opt/local/lib  /opt/local/lib/libboost_unit_test_framework-mt.dylib /opt/local/lib/libboost_system-mt.dylib 
Undefined symbols for architecture x86_64:
....

「未定義のシンボル...」以降の最後のエラーと同じように見えるため、残りのエラーは省略しました。

4

1 に答える 1

3

target_link_libraries に と の両方が${Boost_LIBRARIES}あります。boost_system-mtそれは必要ないはずです。systemあなたがあなたの呼び出しに含めたのでfind_package(Boost ...)、それも中にあるはず${Boost_LIBRARIES}です。

編集後:

64ビットを構築しているようです。ブースト ライブラリは 64 ビットですか? それ以外の場合は、代わりに 32 ビット ビルドを試してください。

別の更新:

この関連する質問を見つけました:アーキテクチャ x86_64 の未定義シンボル: どのアーキテクチャを使用する必要がありますか?

どうやらエラーの理由は、に設定CMAKE_CXX_COMPILERしているためgccです。である必要がありますg++

于 2013-02-07T21:26:43.443 に答える