C++ bug: <fstream> not including correctly

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

C++ bug: <fstream> not including correctly

3,368 次查看
bowerymarc
Contributor V

I'm getting a very odd error in C++, CW 10.4, Kinetis project.  I can't compile an std::ifstream variable!

Taking the Freedom_CPP project from ErichStyger/mcuoneclipse · GitHub

And modifying it, I add an ifstream variable to the class, and #include <fstream> to the top of the file and get this error.

Searching all day for an answer and can't figure out why this doesn't work.

Help!

Here's the modified application.cpp source file, everything else the same:

/*

* main implementation: use this 'C++' sample to create your own application

*

*/

/*#include <stdio.h>*/ /* uncomment to enable 'puts' below */

#include "application.h"

#include <fstream>

class counterclass

{

private:

  int m_counter;

public:

  counterclass(void)

  {

  m_counter = 0;

  };

  void increment(void)

  {

  m_counter++;

  };

  std::ifstream file_;

};

void APP_Run(void)

{

  counterclass myccounter;

  /* Enabling 'puts' below will use the UART/SCI of your target.

  * Make sure you correctly set it up or how to use the Debug Console instead.

  * See the targeting manual for details.

  */

  /*puts("Hello (Kinetis) World in 'C++' from MKL25Z128 derivative!");*/

  for(;;) {

  myccounter.increment();

  }

}

And the console output:

'Building file: ../Sources/application.cpp'

'Executing target #3 ../Sources/application.cpp'

'Invoking: ARM Ltd Windows GCC C++ Compiler'

"C:/Freescale/CW MCU v10.4/Cross_Tools/arm-none-eabi-gcc-4_7_3/bin/arm-none-eabi-g++" "../Sources/application.cpp" @"Sources/application.args" -Wa,-adhlns="Sources/application.o.lst" -MMD -MP -MF"Sources/application.d" -o"Sources/application.o"

../Sources/application.cpp:25:16: error: field 'file_' has incomplete type

mingw32-make: *** [Sources/application.o] Error 1

标记 (2)
0 项奖励
回复
5 回复数

2,196 次查看
snahmad
Contributor II

Does C++ file read works on NXP boards/

 

 

C file I/O fopen is working with Macros

e.g

 

std::ifstream *infile = new std::ifstream();
infile->open ("/language/language_en-GB.xml", std::ifstream::in);

bool is_good = infile->good();

 

is_good is false.

EWL_FILENAME_MAX
EWL_OS_DISK_FILE_SUPPORT

 

Do we need to define different macros for C++ file stream

 

my macros are

 

__NEWLIB__
CPU_MIMXRT1052DVL6B
CPU_MIMXRT1052DVL6B_cm7
SDK_DEBUGCONSOLE=1
XIP_EXTERNAL_FLASH=1
XIP_BOOT_HEADER_ENABLE=1
XIP_BOOT_HEADER_DCD_ENABLE=1
SKIP_SYSCLK_INIT
FSL_SDK_ENABLE_DRIVER_CACHE_CONTROL=1
SD_ENABLED
SERIAL_PORT_TYPE_UART=1
SDK_OS_FREE_RTOS
MCUXPRESSO_SDK
CR_INTEGER_PRINTF
PRINTF_FLOAT_ENABLE=0
__MCUXPRESSO
__USE_CMSIS
DEBUG
EWL_FILENAME_MAX
EWL_OS_DISK_FILE_SUPPORT

https://www.nxp.com/docs/en/reference-manual/CWEWLCPPREF.pdf

0 项奖励
回复

2,687 次查看
bowerymarc
Contributor V

I've reduced this to a bug.  Using a new baremetal C++ project with the following settings:

K20F120X512

c++

no i/o  OR uart i/o

GCC

FLASH target

I still get the same error as above:

C:\Freescale\CW MCU v10.4\gnu\bin\mingw32-make all

'Building file: ../Sources/main.cpp'

'Executing target #1 ../Sources/main.cpp'

'Invoking: ARM Ltd Windows GCC C++ Compiler'

"C:/Freescale/CW MCU v10.4/Cross_Tools/arm-none-eabi-gcc-4_7_3/bin/arm-none-eabi-g++" "../Sources/main.cpp" @"Sources/main.args" -MMD -MP -MF"Sources/main.d" -o"Sources/main.o"

../Sources/main.cpp:25:16: error: field 'file_' has incomplete type

mingw32-make: *** [Sources/main.o] Error 1

And looking at the preprocessor output, the same issue is true... std::fstream and std::basic_fstream have forward declarations but no actual class definition.

template <class charT, class traits = char_traits<charT> >

class basic_fstream;

typedef basic_fstream<char, char_traits<char> > fstream;

Freescale - there's something wrong with the way #include <fstream> is functioning... this should work!

Project is attached.

0 项奖励
回复

2,687 次查看
bowerymarc
Contributor V

OK I think I figured this out. The issue was code being #ifdef'd out due to global settings in various CW setup files.  By turning on file support I got it to compile.  Here's the source code, the comments at top show what I changed to get it to work:

/*

* main implementation: use this 'C++' sample to create your own application

*

*/

/*

  notes: in file ewlconfig_ARM changed:

// #define _EWL_NO_FILE_IO //commented

  #define _EWL_CFILE_STREAM //uncommented

  in file ansi_prefix.ARM.h changed:

  #define _EWL_NO_WCHART_C_SUPPORT //added

*/

#include <niostream> // iostream has wide char support i don't care about

#include <fstream>

#include <cstdlib>

#include <cstdio>

class counterclass

{

private:

  int m_counter;

public: 

  counterclass(void)

  {

  m_counter = 0;

  };

  void increment(void)

  {

  m_counter++;

  }; 

  std::ifstream *file_; //works

  std::ifstream file1_(); //works

  std::ifstream file2_; //doesnt work ("field file1_ has incomplete type")

};

int main()

{

  counterclass myccounter;

  for(;;) {

  myccounter.increment();

  }

  return 0;

}

extern "C"

{

#include <sys/UART.h>

/****************************************************************************/

/*

* Implementation for CodeWarror MSL interface to serial device (UART.h).

* Needed for printf, etc...

* Only InitializeUART, ReadUARTN, and WriteUARTN are implemented.

*

*/

UARTError InitializeUART(UARTBaudRate baudRate)

{

  //todo: add functionality if used

  return kUARTNoError;

}

/****************************************************************************/

/*

  ReadUARTN

  Read N bytes from the UART.

  bytes pointer to result buffer

  limit size of buffer and # of bytes to read

*/

/****************************************************************************/

UARTError ReadUARTN(void* bytes, unsigned long limit)

{

  //todo: add functionality if used

  return kUARTNoError;

}

/****************************************************************************/

UARTError WriteUARTN(const void* bytes, unsigned long length)

{

  //todo: add functionality if used

  return kUARTNoError;

}

} // extern C

2,686 次查看
bowerymarc
Contributor V

another clue.  Based on the code posted above:

  std::ifstream *file_; //works

  std::ifstream file1_(); //works

  std::ifstream file1_; //doesnt work ("field file1_ has incomplete type")

so somehow I can't declare a variable of the class only instantiate?

0 项奖励
回复

2,687 次查看
bowerymarc
Contributor V

just tried the same project in CW 10.5, get the same error. fixes??

0 项奖励
回复