編譯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.5k
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

撰寫答案

請登錄後再發布答案,點擊登錄

發布
問題

分享
好友

手機
浏覽

掃碼手機浏覽