【nRF-C++】 cout 重定向

环境:wsl , gcc , 9.3.1

效果如下

 

extern "C"
{
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    int aaaaa(void);
}

#include 
#include 

using namespace std;

class a
{
private:
    /* data */
public:
    a(/* args */);
    ~a();
    public:
    void p( void ){
        NRF_LOG_INFO("P");
    }
};

a::a(/* args */)
{
    NRF_LOG_INFO("A Constructor");
}

a::~a()
{
    NRF_LOG_INFO("A Destructor");
}

int main(void)
{

    string str1 = "11111";
    string str2 = "22222";

    string str3 = str1+str2;

    aaaaa();

    NRF_LOG_INFO("Start");

    a *aa = new a;

    aa->p();

    cout << str3 << endl;
    delete aa;
    

    NRF_LOG_INFO("End");
    NRF_LOG_INTERNAL_FLUSH();
    while(1);

    return 0;
}

 

【nRF-C++】 cout 重定向_第1张图片

 

 

 

一开始试的是这个, 但是cout 不能输出

#include 


//#pragma import(__use_no_semihosting) // 禁用半主机模式
// 请不要勾选Use MicroLib
#ifdef __MICROLIB
#error "Please do not use MicroLib!"
#endif

extern "C"
{
    
    #include "nrf_error.h"

    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"

    void _sys_exit(int returncode)
    {
        printf("Exited! returncode=%d\n", returncode);
        while (1);
    }

    int __ttyfputc(int ch)
    {

        //UNUSED_VARIABLE(app_uart_put((uint8_t)ch));
        NRF_LOG_RAW_INFO("%c", (uint8_t)ch);
        NRF_LOG_INTERNAL_FLUSH();
        return ch;
    }

    void _ttywrch(int ch)
    {
        //NRF_LOG_INFO("aaaaa");
        __ttyfputc(ch);
    }

    uint8_t  __ttygetch( void )
    {
        return 0;
    }
}

namespace std
{
    struct __FILE
    {
        int handle;
        /* Whatever you require here. If the only file you are using is */
        /* standard output using printf() for debugging, no file handling */
        /* is required. */
    };

    FILE __stdin;
    FILE __stdout;
    FILE __stderr;

    int fgetc(FILE *stream)
    {
        return __ttygetch();
    }

    int fputc(int c, FILE *stream)
    {
        NRF_LOG_RAW_INFO("%c", (uint8_t)c);
        NRF_LOG_INTERNAL_FLUSH();

        return c;
    }

    int fclose(FILE *stream)
    {
        return 0;
    }

    int fseek(FILE *stream, long int offset, int whence)
    {
        return -1;
    }

    int fflush(FILE *stream)
    {
        return 0;
    }
} // namespace std

 

 

 

使用这个 , 加入makefile 后, cout能正常输出

/**
 * Copyright (c) 2014 - 2019, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include "sdk_common.h"
/** @file
 *
 * @defgroup retarget Retarget layer for stdio functions
 * @{
 * @ingroup app_common
 * @} */


#include 
#include 

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#if defined(__CC_ARM)

// This part is taken from MDK-ARM template file and is required here to prevent
// linker from selecting libraries functions that use semihosting and failing
// because of multiple definitions of fgetc() and fputc().
// Refer to: http://www.keil.com/support/man/docs/gsac/gsac_retargetcortex.htm
// -- BEGIN --
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
// --- END ---

int fgetc(FILE * p_file)
{
    return 0;
}

int fputc(int ch, FILE * p_file)
{
    UNUSED_PARAMETER(p_file);

    NRF_LOG_RAW_INFO("%c", (uint8_t)ch);
    NRF_LOG_INTERNAL_FLUSH();
    return ch;
}

#elif defined(__GNUC__) && defined(__SES_ARM)

int __getchar(FILE * p_file)
{
    return 0;
}

#if defined(__SES_VERSION) && (__SES_VERSION >= 34000)
int __putchar(int ch, __printf_tag_ptr tag_ptr)
{
    UNUSED_PARAMETER(tag_ptr);

    NRF_LOG_RAW_INFO("%c", (uint8_t)ch);
    NRF_LOG_INTERNAL_FLUSH();
    return ch;
}
#else
int __putchar(int ch, FILE * p_file)
{
    UNUSED_PARAMETER(p_file);

    NRF_LOG_RAW_INFO("%c", (uint8_t)ch);
    NRF_LOG_INTERNAL_FLUSH();
    return ch;
}
#endif

#elif defined(__GNUC__) && !defined(__SES_ARM)

int _write(int file, const char * p_char, int len)
{
    int i;

    UNUSED_PARAMETER(file);

    for (i = 0; i < len; i++)
    {
        NRF_LOG_RAW_INFO("%c", (uint8_t)*p_char++);
    }
    NRF_LOG_INTERNAL_FLUSH();

    return len;
}

int _read(int file, char * p_char, int len)
{
    UNUSED_PARAMETER(file);

    return 1;
}
#elif defined(__ICCARM__)

size_t __write(int handle, const unsigned char * buffer, size_t size)
{
    int i;
    UNUSED_PARAMETER(handle);
    for (i = 0; i < size; i++)
    {
        NRF_LOG_RAW_INFO("%c", (uint8_t)*buffer++);
    }

    NRF_LOG_INTERNAL_FLUSH();

    return size;
}

size_t __read(int handle, unsigned char * buffer, size_t size)
{
    UNUSED_PARAMETER(handle);
    UNUSED_PARAMETER(size);

    return 1;
}

long __lseek(int handle, long offset, int whence)
{
    return -1;
}
int __close(int handle)
{
    return 0;
}
int remove(const char * filename)
{
    return 0;
}

#endif

 

你可能感兴趣的:(nRF-C++)