c++ - 如何解决makefile中main的重定义

标签 c++ makefile

我正在尝试使用 makefile 将三个文件编译并链接到一个可执行文件中,但似乎重新定义了 main 或以某种方式搞砸了编译/链接过程。该项目是一个类,目标是实现线性反馈移位寄存器,但我们必须使用 makefile。

我在哪里重新定义了 main?如何更改我的 makefile 以创建我的可执行文件?我注意到 test.o 的错误指向重新定义了 main,但我不确定为什么或如何。

错误:

g++ -c main.cpp LFSR.cpp -Wall -Werror -ansi -pedantic
g++ -c test.cpp -Wall -Werror -ansi -pedantic
g++ main.o LFSR.o test.o -o ps2a -lboost_unit_test_framework
test.o: In function `main':
test.cpp:(.text+0xa3): multiple definition of `main'
main.o:main.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
Makefile:4: recipe for target 'ps2a' failed
make: *** [ps2a] Error 1

我的生成文件:

all: ps2a

ps2a: main.o LFSR.o test.o
     g++ main.o LFSR.o test.o -o ps2a -lboost_unit_test_framework

LFSR.o: LFSR.cpp LFSR.hpp
     g++ -c LFSR.cpp -Wall -Werror -ansi -pedantic

main.o: main.cpp LFSR.hpp
    g++ -c main.cpp LFSR.cpp -Wall -Werror -ansi -pedantic

test.o: test.cpp
    g++ -c test.cpp -Wall -Werror -ansi -pedantic

clean:
    rm *.o ps2a

主要.cpp:

#include "LFSR.hpp"

int main(){
}

LFSR.hpp

#include <string>
#include <iostream>

class LFSR{
public:
    LFSR(std::string, int);
    int step();
    int generate(int k);
private:
    std::string bitString;
    int tapPos;
};

LFSR.cpp:

#include "LFSR.hpp"

void makeBitStringValid(std::string& str);

LFSR::LFSR(std::string str, int t){
}

int LFSR::step(){
    return 0;
}

int LFSR::generate(int k){
    return 0;
}

void makeBitStringValid(std::string& str){
}

test.cpp(注意,这是导师给的——我还不完全确定它是如何工作的)

#include <iostream>
#include <string>

#include "LFSR.hpp"

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Main
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(fiveBitsTapAtTwo) {

  LFSR l("00111", 2);
  BOOST_REQUIRE(l.step() == 1);
  BOOST_REQUIRE(l.step() == 1);
  BOOST_REQUIRE(l.step() == 0);
  BOOST_REQUIRE(l.step() == 0);
  BOOST_REQUIRE(l.step() == 0);
  BOOST_REQUIRE(l.step() == 1);
  BOOST_REQUIRE(l.step() == 1);
  BOOST_REQUIRE(l.step() == 0);

  LFSR l2("00111", 2);
  BOOST_REQUIRE(l2.generate(8) == 198);
}

最佳答案

不要提供自己的 main,因为 Boost 单元测试框架已经在您的 test.cpp 中提供了一个,其中包含以下行:

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Main
#include <boost/test/unit_test.hpp>

Dynamic library variant of the UTF

Unlike the static library variant function main() can't reside in the dynamic library body. Instead this variant supplies default function main() implementation as part of the header boost/test/unit_test.hpp to be generated as part of your test file body. The function main() is generated only if either the BOOST_TEST_MAIN or the BOOST_TEST_MODULE flags are defined during a test module compilation. For single-file test module flags can be defined either in a test module's makefile or before the header boost/test/unit_test.hpp inclusion. For a multi-file test module flags can't be defined in makefile and have to be defined in only one of the test files to avoid duplicate copies of the function main().

关于c++ - 如何解决makefile中main的重定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48625038/

相关文章:

c++ - 防止 Boost Spirit Symbol 解析器过早接受关键字

c++ - 在 VxWorks 中使用 Boost C++ 库时出错

makefile - 反转make变量中字符串顺序的最简单方法

git - 带有绝对路径的 Makefile 和 Git

c++ - 为什么枚举类的默认类型与枚举的基础类型不同?

c++ - 这是生成 GUID 的可靠方法吗?

c++ - 为什么我尝试解决UVA 624的代码没有给出正确的输出?

linux - Makefile 错误地混淆了不存在的文件夹

makefile - 限制子make中的并发

linux - 通配符不适用于新生成的文件夹