c++ - 由于多个 'main',STM8上的cpputest失败

标签 c++ c cpputest stm8

我想在STM8上使用cpputest并为此安装了所有必需的工具。
我可以在简化代码上运行cpputest。
在属于硬件的主文件上,我当然拥有主功能。但是在测试环境中,我在AllTests.cpp下也有一个主要功能。当我编译它我得到错误:

multiple definition of `main'

我遇到的另一个问题是:我为8位处理器编译了代码,并使用了<stdint.h>库,因此我的主文件包含uint8_t main(){行。 cpputest的编译器根本不喜欢这样。。。

有谁知道如何解决这个问题?

文件:

blinky.h:
#ifndef BLINKY_H
#define BLINKY_H

#include "stm8l.h"
#include <stdint.h>

uint16_t blink(void);

#endif

眨眼
#include "blinky.h"


uint16_t blink(){
    PD_DDR = 0x1;
    PD_CR1 = 0x1;
    return 1;
}
uint8_t main() {
    // Configure pins
    while(1){
    // Loop
    blink();
    }
}

test.cpp:
#include "CppUTest/TestHarness.h"

extern "C"
{
#include "blinky.h"
}

TEST_GROUP(FirstTestGroup)
{
    void setup()
    {
    }

    void teardown()
    {
    }
};

TEST(FirstTestGroup, test1)
{
    LONGS_EQUAL(1, blink());
}

AllTest.cpp:
#include "CppUTest/CommandLineTestRunner.h"

int main(int ac, char** av)
{
    return CommandLineTestRunner::RunAllTests(ac, av);
}

生成文件:
#Set this to @ to keep the makefile quiet
SILENCE = @

#---- Outputs ----#
COMPONENT_NAME = blinky

#--- Inputs ----#
PROJECT_HOME_DIR = .
ifeq "$(CPPUTEST_HOME)" ""
    CPPUTEST_HOME = ~/tools/cpputest
endif

# --- SRC_FILES ---
# Use SRC_FILES to specifiy individual production
# code files.
# These files are compiled and put into the
# ProductionCode library and links with the test runner
SRC_FILES = src/blinky.c

# --- SRC_DIRS ---
# Use SRC_DIRS to specifiy production directories
# code files.
# These files are compiled and put into a the
# ProductionCode library and links with the test runner
SRC_DIRS = \
    platform

# --- TEST_SRC_FILES ---
# TEST_SRC_FILES specifies individual test files to build.  Test
# files are always included in the build and they
# pull in production code from the library
TEST_SRC_FILES = \

# --- TEST_SRC_DIRS ---
# Like TEST_SRC_FILES, but biulds everyting in the directory
TEST_SRC_DIRS = \
    tests \
    #tests/blinky \
    #tests/io-cppumock \
    #tests/exploding-fakes \
    #tests \
    #tests/example-fff \
    #tests/fff \
# --- MOCKS_SRC_DIRS ---
# MOCKS_SRC_DIRS specifies a directories where you can put your
# mocks, stubs and fakes.  You can also just put them
# in TEST_SRC_DIRS
MOCKS_SRC_DIRS = \

# Turn on CppUMock
CPPUTEST_USE_EXTENSIONS = Y

INCLUDE_DIRS =\
  .\
  $(CPPUTEST_HOME)/include/ \
  $(CPPUTEST_HOME)/include/Platforms/Gcc \
  platform \
  src \
  include \
  #example-fff \
  #test/exploding-fakes \
  #tests/fff


#STM8DIR

#SDCC_DIR :=$(CPPUTEST_HOME)/../sdcc/
#CC       :=@$(SDCC_DIR)/bin/sdcc
# --- CPPUTEST_OBJS_DIR ---
# if you have to use "../" to get to your source path
# the makefile will put the .o and .d files in surprising 
# places.
# To make up for each level of "../", add place holder 
# sub directories in CPPUTEST_OBJS_DIR
# each "../".  It is kind of a kludge, but it causes the
# .o and .d files to be put under objs.
# e.g. if you have "../../src", set to "test-objs/1/2"
# This is set no "../" in the source path.
CPPUTEST_OBJS_DIR = test-obj

CPPUTEST_LIB_DIR = test-lib
CPPUTEST_WARNINGFLAGS += -Wall
CPPUTEST_WARNINGFLAGS += -Werror
CPPUTEST_WARNINGFLAGS += -Wswitch-default
CPPUTEST_WARNINGFLAGS += -Wfatal-errors
CPPUTEST_CXXFLAGS = -Wno-c++14-compat
CPPUTEST_CFLAGS = -std=c99
CPPUTEST_CXXFLAGS += $(CPPUTEST_PLATFORM_CXXFLAGS)
CPPUTEST_CFLAGS += -Wno-missing-prototypes 
CPPUTEST_CXXFLAGS += -Wno-missing-variable-declarations
# --- LD_LIBRARIES -- Additional needed libraries can be added here.
# commented out example specifies math library
#LD_LIBRARIES += -lm

# Look at $(CPPUTEST_HOME)/build/MakefileWorker.mk for more controls

include $(CPPUTEST_HOME)/build/MakefileWorker.mk

最佳答案

正如建议中所建议的那样,没有其他方法可以为主 Controller 创建仅具有Controller的文件并将其与单元测试分开。

我的结构如下:
main.c:
-包含固件(app.h)和main()中的包含内容以及run_app()
app.c:
包含所有固件,并通过cpputest测试

关于c++ - 由于多个 'main',STM8上的cpputest失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47181446/

相关文章:

c++ - 我如何在 C++ 中编写哈希函数?

c++ - Liskov 替换原则和游戏的类设计

c++ - 正则表达式 ("(abc|aa.*|bb.*)") 与正则表达式 ("(aa.*|bb.*|cc.*)");

c - C 中的段错误导致代码有时运行但有时不运行

c++ - 在我的 https 服务器中禁用 DES

CppUtest示例链接错误

c++ - 检查两个 vector 是否相等

c++ - 代码在 WinDbg 上运行良好,但没有它就很奇怪

c - fork 如何与逻辑运算符一起工作

c - 在 CppUTest 中用 "test"定义替换定义