编译C++文件构造不执行

发布于 2022-08-22 22:56:44

【软件版本】
CDK 2.10.1
【背景】
在W806中使用C++方式编程
【问题】
在一个类中定义一个构造函数,在main.cpp中声明一个全局实例,构造函数不执行(此问题在我适配的Arduino的时候也同样遇到 不知道怎么联系你们只能在此发帖)

print.c

#include "print.h"
#include <stdio.h>
#include "wm_hal.h"
os_print My_print;

os_print::os_print()
{
    SystemClock_Config(CPU_CLK_160M);
//    HAL_Init();
    HAL_Init();
    //GPIO_Init();
        GPIO_InitTypeDef GPIO_InitStruct = {0};
    
    __HAL_RCC_GPIO_CLK_ENABLE();

    GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2, GPIO_PIN_SET);
    
    GPIO_InitStruct.Pin = GPIO_PIN_5;
    GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    
    HAL_NVIC_SetPriority(GPIOB_IRQn, 0);
    HAL_NVIC_EnableIRQ(GPIOB_IRQn);
    
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0,GPIO_PIN_RESET);
            HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1,GPIO_PIN_SET);
            HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2,GPIO_PIN_SET);
    
    printf("file[%s] line[%d] \r\n", __FILE__, __LINE__);
}

os_print::os_print(uint16_t x)
{
    
    printf("file[%s] line[%d] init num[%d]\r\n", __FILE__, __LINE__, x);
}

void os_print::my_print(uint16_t x)
{
    
    printf("my_print num[%d]\r\n", x);
    
}

print.h

#ifndef __PRINT_H__
#define __PRINT_H__
#include "stdint.h"
#include "wm_hal.h"


class os_print 
{
    
public:
    os_print();
    os_print(uint16_t x);    
    void my_print(uint16_t x);
    
};



extern os_print My_print;

#endif

main.c


#include <stdio.h>
#include "wm_hal.h"
#include "print.h"

void Error_Handler(void);
static void GPIO_Init(void);

static volatile uint8_t key_flag = 0;


int main(void)
{

    SystemClock_Config(CPU_CLK_160M);
    printf("enter main\r\n");
    printf("hello world\r\n");
    HAL_Init();
    GPIO_Init();

    My_print.my_print(12);
    
    
    while(1)
    {
        
        
    }
        
    
    return 0;
}

查看更多

关注者
0
被浏览
1.3k
2 个回答
Argon
Argon 2022-08-22
这家伙很懒,什么也没写!

@isme @lutherluo 老板们请求支援

ZYQ
ZYQ 2022-08-22
我已不再支持W80X任何相关问题的回复,请大家不要私信,有问题找 isme 谢谢

W806的默认SDK 是不支持C++ 的,要想支持C++ 需要修改链接脚本文件,修改方式也比较简单,另外文件名称需要改成CPP 才可以,
参考这样做一下就可以了
http://ask.winnermicro.com/article/69.html

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览