[英]How to use boost tests with SFML and cmake?
我正在使用 SFML 在 C++ 中編寫一個簡單的游戲。 我想使用增強測試,但是當我嘗試在我使用 SFML 功能的每個地方獲得未定義的引用時(游戲本身運行良好,只是測試沒有)。 樣品測試:
#include <boost/test/unit_test.hpp>
#include "Sentry.h"
BOOST_AUTO_TEST_SUITE(BasicModelTestSuite)
BOOST_AUTO_TEST_CASE(testLamp_10BulbsWithPower10_ExpectedPower)
{
sf::Texture projectileTexture;
sf::Texture sentryTexture;
std::vector<std::shared_ptr<Sentry>> foes;
sentryTexture.loadFromFile("../GameResources/sentry_image.png");
projectileTexture.loadFromFile("../GameResources/projectile_animation.png");
foes.push_back(std::make_shared<Sentry>(Sentry(&sentryTexture, sf::Vector2u(1,2), 0.2f, sf::Vector2f(140,200), projectileTexture, true)));
foes.push_back(std::make_shared<Sentry>(Sentry(&sentryTexture, sf::Vector2u(1,2), 0.2f, sf::Vector2f(200,200), projectileTexture, false)));
BOOST_REQUIRE_EQUAL(foes.size(), 2);
}
BOOST_AUTO_TEST_SUITE_END()
以及 cmakelist.txt 的測試部分:
ind_package(Boost 1.65 COMPONENTS "unit_test_framework" REQUIRED)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/include
${Boost_INCLUDE_DIRS}
)
set(SOURCE_TEST_FILES
test/master.cpp test/master.cpp test/baseTest.cpp
main.cpp src/Level.cpp include/Level.h src/Game.cpp include/Game.h src/Platform.cpp include/Platform.h src/Item.cpp include/Item.h src/Life.cpp include/Life.h src/Coin.cpp include/Coin.h src/Collider.cpp include/Collider.h src/Enemy.cpp include/Enemy.h src/MrStrawberry.cpp include/MrStrawberry.h src/Animation.cpp include/Animation.h src/MrBerry.cpp include/MrBerry.h src/Projectile.cpp include/Projectile.h src/Sentry.cpp include/Sentry.h)
add_executable(TestProject ${SOURCE_TEST_FILES})
target_include_directories(TestProject PUBLIC include)
target_link_libraries(TestProject
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
enable_testing()
add_test(NAME Test COMMAND TestProject
--report_level=detailed
--log_level=all
--color_output=yes)
add_custom_target(check ${CMAKE_COMMAND} -E env CTEST_OUTPUT_ON_FAILURE=1 BOOST_TEST_LOG_LEVEL=all
${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION> --verbose
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
我不知道為什么它會拋出對所有內容的未定義引用,如下所示:
/usr/bin/ld: /home/student/CLionProjects/oop21_ww2_14/BerryGame/src/Projectile.cpp:8: undefined reference to `sf::RectangleShape::setSize(sf::Vector2<float> const&)'
/usr/bin/ld: /home/student/CLionProjects/oop21_ww2_14/BerryGame/src/Projectile.cpp:9: undefined reference to `sf::Shape::setTexture(sf::Texture const*, bool)'
TestProject 在鏈接選項中缺少 SFML 庫(另請參閱什么是未定義的引用/未解析的外部符號錯誤以及如何修復它? )。 您的主要可執行文件顯然有它,但您沒有顯示它。
尋找類似的東西
LINK_LIBRARIES(sfml-system sfml-window sfml-graphics sfml-audio)
或者改為 target_link_libraries。 如果使用LINK_LIBRARIES
,我想目標可能需要在那之后定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.