Day: November 3, 2016

Discovering where a function “already declared in this scope” was declared

During the compilation you face this issue:

make[2]: Entering directory '/home/alan/nuttxspace/apps/examples/helloxx'
CXX:  helloxx_main.cxx
In file included from /usr/include/newlib/c++/4.9.3/bits/char_traits.h:380:0,
                 from /usr/include/newlib/c++/4.9.3/string:40,
                 from helloxx_main.cxx:42:
/home/alan/nuttxspace/nuttx/include/cxx/cstdint:75:11: error: 'uint_least32_t' is already declared in this scope
   using ::uint_least32_t;
           ^
/home/alan/nuttxspace/apps/Application.mk:85: recipe for target 'helloxx_main.o' failed
make[2]: *** [helloxx_main.o] Error 1
make[2]: Leaving directory '/home/alan/nuttxspace/apps/examples/helloxx'
Makefile:92: recipe for target 'examples/helloxx_all' failed
make[1]: *** [examples/helloxx_all] Error 2
make[1]: Leaving directory '/home/alan/nuttxspace/apps'
LibTargets.mk:174: recipe for target '../apps/libapps.a' failed
make: *** [../apps/libapps.a] Error 2

First thing to do is compile in verbose mode (make V=1) :

$ make V=1
...
make[2]: Entering directory '/home/alan/nuttxspace/apps/examples/helloxx'
CXX:  helloxx_main.cxx
arm-none-eabi-g++ -c -fno-builtin -fno-use-cxa-atexit -fcheck-new -specs=nano.specs -lstdc++ -lsupc++ -lm -lgcc -lc -lnosys -std=gnu++11 -Wall -Wshadow -Wundef -g -mcpu=cortex-m4 -mthumb -mfloat-abi=soft -I. -isystem /home/alan/nuttxspace/nuttx/include -isystem /home/alan/nuttxspace/nuttx/include/cxx   -pipe -I "/home/alan/nuttxspace/apps/include"  helloxx_main.cxx -o  helloxx_main.o
In file included from /usr/include/newlib/c++/4.9.3/bits/char_traits.h:380:0,
                 from /usr/include/newlib/c++/4.9.3/string:40,
                 from helloxx_main.cxx:42:
/home/alan/nuttxspace/nuttx/include/cxx/cstdint:75:11: error: 'uint_least32_t' is already declared in this scope
   using ::uint_least32_t;
           ^
/home/alan/nuttxspace/apps/Application.mk:85: recipe for target 'helloxx_main.o' failed
make[2]: *** [helloxx_main.o] Error 1
make[2]: Leaving directory '/home/alan/nuttxspace/apps/examples/helloxx'
Makefile:92: recipe for target 'examples/helloxx_all' failed
make[1]: *** [examples/helloxx_all] Error 2
make[1]: Leaving directory '/home/alan/nuttxspace/apps'
LibTargets.mk:174: recipe for target '../apps/libapps.a' failed
make: *** [../apps/libapps.a] Error 2

Copy the above g++ command line:

arm-none-eabi-g++ -c -fno-builtin -fno-use-cxa-atexit -fcheck-new -specs=nano.specs -lstdc++ -lsupc++ -lm -lgcc -lc -lnosys -std=gnu++11 -Wall -Wshadow -Wundef -g -mcpu=cortex-m4 -mthumb -mfloat-abi=soft -I. -isystem /home/alan/nuttxspace/nuttx/include -isystem /home/alan/nuttxspace/nuttx/include/cxx   -pipe -I "/home/alan/nuttxspace/apps/include"  helloxx_main.cxx -o  helloxx_main.o

Enter inside the directory where the issue happened:

$ cd ../apps/examples/helloxx/

Paste the g++ line we copied and add “-save-temps” to it:

alan@pc:~/nuttxspace/apps/examples/helloxx$ arm-none-eabi-g++ -c -fno-builtin -fno-use-cxa-atexit -fcheck-new -specs=nano.specs -lstdc++ -lsupc++ -lm -lgcc -lc -lnosys -std=gnu++11 -Wall -Wshadow -Wundef -g -mcpu=cortex-m4 -mthumb -mfloat-abi=soft -I. -isystem /home/alan/nuttxspace/nuttx/include -isystem /home/alan/nuttxspace/nuttx/include/cxx   -pipe -I "/home/alan/nuttxspace/apps/include"  helloxx_main.cxx -o  helloxx_main.o -save-temps
arm-none-eabi-g++: warning: -pipe ignored because -save-temps specified
In file included from /usr/include/newlib/c++/4.9.3/string:40:0,
                 from helloxx_main.cxx:42:
/usr/include/newlib/c++/4.9.3/bits/char_traits.h:63:20: error: 'mbstate_t' in namespace 'std' does not name a type
       typedef std::mbstate_t  state_type;
                    ^
In file included from /usr/include/newlib/c++/4.9.3/bits/char_traits.h:380:0,
                 from /usr/include/newlib/c++/4.9.3/string:40,
                 from helloxx_main.cxx:42:
/home/alan/nuttxspace/nuttx/include/cxx/cstdint:75:11: error: 'uint_least32_t' is already declared in this scope
   using ::uint_least32_t;
           ^

Confirm the .ii file was created:

$ ls *.ii
helloxx_main.ii

Open it:

$ vi helloxx_main.ii

Analyzing it we found:


# 42 "/usr/include/newlib/c++/4.9.3/type_traits" 3
namespace std
{
  typedef short unsigned int uint_least16_t;
  typedef long unsigned int uint_least32_t;
}


# 219 "/home/alan/nuttxspace/nuttx/include/stdint.h" 3 4
...
typedef _uint32_t uint_least32_t;


# 63 "/usr/include/newlib/machine/_default_types.h" 3 4
...
typedef long unsigned int __uint32_t;

Good, now we know the places where uint_least32_t was re-defined.

Thanks Greg Nutt for this tip!