c++ - 处理多个功能文件时 Cucumber-cpp 上的链接器错误

标签 c++ cucumber bdd linker-errors cucumber-cpp

我一直在使用 BDD 框架 Cucumber-cpp 一段时间,现在没有太多问题,但事实证明扩展模块的功能集会引发一些链接器错误。

设置:

  • Linux
  • cucumber -cpp
  • 生成文件

如果我从以下开始:

# ./test1.feature
Feature: test with a single cpp file

Scenario:
    Given I'm a test
    When a trig happens
    Then light should be ON

// ./step_definitions/f1.cpp
#include <boost/test/unit_test.hpp>
#include <cucumber-cpp/defs.hpp>

#include <iostream>

using namespace std;

GIVEN("I'm a test")
{
    cerr << "Given..." << endl;
}

WHEN("a trig happens")
{
    cerr << "Trig!" << endl;
}

THEN("light should be on")
{
    cerr << "light ON..." << endl;
}

当我用 make 构建它时,它生成测试二进制文件没有问题,并且通过 Cucumber 执行按预期工作。

但是如果我添加另一个特性文件(它描述了另一个要测试的特性)

# ./test2.feature
Feature: another set of feature to impl.

Scenario: extra code
    Given I'm another test code
    When a trig happens
    Then light should be OFF

与相关的测试文件,

// ./step_definitions/f2.cpp
#include <boost/test/unit_test.hpp>
#include <cucumber-cpp/defs.hpp>

#include <iostream>

using namespace std;

GIVEN("I'm another test")
{
    cerr << "Given ANOTHER..." << endl;
}

现在编译这 2 个文件会出现以下链接器错误:

$  make build
mkdir -p ./objs
mkdir -p ./build
compiling step_definitions/f1.cpp
gcc -c -g -O0 -DDEBUG -Wall -Werror  -DBOOST_ALL_DYN_LINK  -I/usr/local/include -o objs/f1.o -c step_definitions/f1.cpp
compiling step_definitions/f2.cpp
gcc -c -g -O0 -DDEBUG -Wall -Werror  -DBOOST_ALL_DYN_LINK  -I/usr/local/include -o objs/f2.o -c step_definitions/f2.cpp
building feature-test...
gcc  -DBOOST_ALL_DYN_LINK  -I/usr/local/include  -L/usr/local/lib -o feature-test objs/f1.o objs/f2.o -lcucumber-cpp -lboost_regex -lboost_system -lboost_thread -lboost_unit_test_framework -lboost_date_time   
objs/f2.o: In function `toSourceString':
/usr/local/include/cucumber-cpp/internal/step/StepManager.hpp:161: multiple definition of `CukeObject0::cukeRegId'
objs/f1.o:/usr/local/include/cucumber-cpp/internal/step/StepManager.hpp:161: first defined here
objs/f2.o: In function `CukeObject0::body()':
/home/fred/dev/cuketest/step_definitions/f2.cpp:16: multiple definition of `CukeObject0::body()'
objs/f1.o:/home/fred/dev/cuketest/step_definitions/f1.cpp:15: first defined here
collect2: ld returned 1 exit status
make: *** [build] Error 1

查看生成的 obj 文件,给出以下内容:

$  nm objs/f2.o | grep "cukeRegId"
0000000000000395 t _GLOBAL__sub_I__ZN11CukeObject09cukeRegIdE
0000000000000000 B _ZN11CukeObject09cukeRegIdE
$  nm objs/f1.o | grep "cukeRegId"
000000000000069e t _GLOBAL__sub_I__ZN11CukeObject09cukeRegIdE
0000000000000000 B _ZN11CukeObject09cukeRegIdE
0000000000000004 B _ZN11CukeObject19cukeRegIdE
0000000000000008 B _ZN11CukeObject29cukeRegIdE

问题看起来 cukeRegId 函数在 2 个 obj 文件中具有完全相同的修饰名称,导致链接器错误。 现在,我不知道如何防止这种情况发生。

最佳答案

在网上搜索这个,我发现了一个 bug entry与该特定问题相关。 事实证明,在每个 cpp 文件中,我们必须为 CUKE_OBJECT_PREFIX 声明一个唯一的定义值,以避免在链接器时发生对象命名冲突。

关于c++ - 处理多个功能文件时 Cucumber-cpp 上的链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14320148/

相关文章:

c++ - 当类 A 的析构函数是虚函数或非虚函数时,为什么 B 的类成员 n 具有不同的值?

angular - Protractor 无法在已部署的应用程序上检测到 Angular 5

ruby-on-rails - 带有文件、参数和标题字段的 RSpec Post

java - Karate BDD 框架通过简单的配置抛出 NPE

ruby - 如何使用 RSpec 模拟/ stub 文件目录及其内容?

c# - 从 C# 调用 C++ 函数,具有大量复杂的输入和输出参数

c++ - 空间中的相交矩形

C++ 版本高亮显示

html - 如何测试 Cucumber 中的 mailto 链接?

c# - WatiN pressTab 不按 Tab