簡體   English   中英

如何讓 Scons 中的 SConscript 知道 header 位置?

[英]How to make a SConscript in Scons aware of a header location?

我正在嘗試將一個模塊添加到基於Godot的游戲項目中。 我希望使用doctest添加單元測試。 為簡單起見,我將使用上面鏈接中給出的示例。 所以我創建了這個簡單的文件結構:

summator/
    include/
        summator.h
    src/ 
        summator.cpp
    tests/
        doctest.h
        summator_tests.cpp
        SCsub
    config.py
    register_types.h
    register_types.cpp
    SConstruct/SCsub

Godot 需要最后四個文件。 這是SConstruct / SCsub (Godot需要第二個文件名,我使用SConstruct文件單獨測試):

#!/usr/bin/env python

# import the environment provided by Godot
Import( 'env' )
# when tested in isolation, replace with following line
# env = Environment()

# add include directory to the search path 
env.Append( CPPPATH = [ '#include' ] ) # relative path

# add all cpp files so Scons can build the module
env.add_source_files( env.modules_sources, 'src/*.cpp' )
env.add_source_files( env.modules_sources, '*.cpp' )

# if tests are enabled, build them 
if env[ 'tests' ]:
    SConscript([ 'tests/SCsub' ])

接下來, summator summator/tests/中的 SConscript 應該構建測試:

#!/usr/bin/env python

Import( 'env' )

tests_env = env.Clone()

# build tests
tests = test_env.Program( 'runTest', Glob( '*.cpp' )

當我嘗試運行它時,我收到一個錯誤,即找不到 header 'summator.h':

[ 99%] Compiling ==> modules/summator/tests/sumator_tests.cpp
modules/summator/tests/summator_tests.cpp:3:10: fatal error: 'summator.h' file not found
#include "summator.h"
         ^~~~~~~~~~~~~
1 error generated.
scons: *** [modules/summator/tests/summator_tests.linuxbsd.tools.64.llvm.o] Error 1
scons: building terminated because of errors.

我理解 scons 環境的方式,一旦我添加了一個路徑/文件,所有 SConscript(導入環境)都可以訪問它。 我究竟做錯了什么? 這感覺應該是微不足道的,但由於某種原因它不起作用。

為了完整起見,以下是其他源文件:

// summator.h
#ifndef SUMMATOR_H
#define SUMMATOR_H

#include "core/reference.h"

class Summator : public Reference 
{
    GDCLASS( Summator, Reference );

    int count;

protected:
    static void _bind_methods();

public:
    void add( int p_value );
    void reset(); 
    int get_total() const;

    Summator();
};

#endif // SUMMATOR_H
// summator.cpp
#include "summator.h"

void Summator::add( int p_value )
{
    count += p_value;
}

void Summator::reset()
{
    count = 0;
}

int Summator::get_total() const 
{
    return count;
}

void Summator::_bind_methods()
{
    ClassDB::bind_method( D_METHOD( "add", "value" ), &Summator::add );
    ClassDB::bind_method( D_METHOD( "reset" ), &Summator::reset );
    ClassDB::bind_method( D_METHOD( "get_total" ), &Summator::get_total );
}

Summator::Summator()
{
    count = 0;
}
// summator_tests.cpp
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
// #include "thirdparty/doctest/doctest.h"
#include "summator.h"
#include "doctest.h"

TEST_CASE( "testing the summator" )
{
    // class under test
    Summator* cut = new Summator();

    cut->add(10);
    CHECK( cut->get_total() == 10 )
    cut->add(10);
    CHECK( cut->get_total() == 20 )
    cut->add(10);
    CHECK( cut->get_total() == 30 )
    cut->reset();
    CHECK( cut->get_total() == 0 )

    // clean up 
    delete cut;
}

經過一些試驗,我找到了一個可行的解決方案,請查看我的Github 存儲庫中的完整代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM