如何解决makefile中main重定义的问题

Uri*_*law 0 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
Run Code Online (Sandbox Code Playgroud)

我的生成文件:

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
Run Code Online (Sandbox Code Playgroud)

主.cpp:

#include "LFSR.hpp"

int main(){
}
Run Code Online (Sandbox Code Playgroud)

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;
};
Run Code Online (Sandbox Code Playgroud)

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){
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

Mih*_*ayl 5

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

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Main
#include <boost/test/unit_test.hpp>
Run Code Online (Sandbox Code Playgroud)

UTF 的动态库变体

不像静态库变体函数 main() 不能驻留在动态库体中。相反,此变体提供默认函数 main() 实现作为标题的boost/test/unit_test.hpp 一部分,作为测试文件正文的一部分生成。只有在测试模块编译期间定义了 BOOST_TEST_MAIN 或 BOOST_TEST_MODULE 标志时,才会生成函数 main()。对于单文件测试模块标志可以在测试模块的 makefile 中或在头文件boost/test/unit_test.hpp包含之前定义。对于多文件测试模块标志不能在 makefile 中定义,而只能在一个测试文件中定义以避免函数 main() 的重复副本。