简体   繁体   English

如何使用 SFML 和 cmake 进行增强测试?

[英]How to use boost tests with SFML and cmake?

I'm writing a simple game in C++ with use of SFML.我正在使用 SFML 在 C++ 中编写一个简单的游戏。 I want to use boost tests, but when i try to i get undefined reference in every place i use SFML features (the game itself works fine, it's just the test that don't).我想使用增强测试,但是当我尝试在我使用 SFML 功能的每个地方获得未定义的引用时(游戏本身运行良好,只是测试没有)。 Sample test:样品测试:

#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()

And the test portion of cmakelist.txt:以及 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})

I have no clue why it throws undefined reference to everything, like this:我不知道为什么它会抛出对所有内容的未定义引用,如下所示:

/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 is missing the SFML library in the link options (see also What is an undefined reference/unresolved external symbol error and how do I fix it? ). TestProject 在链接选项中缺少 SFML 库(另请参阅什么是未定义的引用/未解析的外部符号错误以及如何修复它? )。 Your main executable apparently does have it, but you don't show it.您的主要可执行文件显然有,但您没有显示它。

Look for something like寻找类似的东西

 LINK_LIBRARIES(sfml-system sfml-window sfml-graphics sfml-audio)

Or target_link_libraries instead.或者改为 target_link_libraries。 If LINK_LIBRARIES is used, I suppose the target might need to be defined after that point.如果使用LINK_LIBRARIES ,我想目标可能需要在那之后定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM