C++ 处理多个功能文件时Cumber cpp上的链接器错误

C++ 处理多个功能文件时Cumber cpp上的链接器错误,c++,cucumber,bdd,linker-errors,cucumber-cpp,C++,Cucumber,Bdd,Linker Errors,Cucumber Cpp,我使用BDD框架Cucumber cpp已经有一段时间了,但没有太多问题,但是扩展模块的功能集会导致一些链接器错误 设置: Linux 黄瓜蛋白 生成文件 如果我从以下内容开始: # ./test1.feature Feature: test with a single cpp file Scenario: Given I'm a test When a trig happens Then light should be ON 及 与相关联的测试文件 // ./s

我使用BDD框架Cucumber cpp已经有一段时间了,但没有太多问题,但是扩展模块的功能集会导致一些链接器错误

设置:

  • Linux
  • 黄瓜蛋白
  • 生成文件
如果我从以下内容开始:

# ./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/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;
}
查看生成的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文件中具有完全相同的修饰名称,从而导致链接器错误。
现在,我一直在思考如何防止这种情况发生。

在互联网上找到了一个与该特定问题相关的问题。
事实证明,在每个cpp文件中,我们必须为CUKE_OBJECT_PREFIX声明一个唯一的define值,以避免链接器时对象命名冲突。

我知道它是旧线程。但我只想强调上面提到的最简单的解决方案,那个就是将cuke步骤定义放在匿名名称空间中

// ./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;
}
$  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
$  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