Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
21 years ago

C++ Application

I was hoping someone out there is using Standard C++ on the Nios II. I can compile, link, and add to the filesystem the following code:

# include <stdio.h>

// hello_nios.cpp

int main(int argc, char * argv[]) {

printf("Hello, Nios\n");

return 0;

}

I can execute this succesfully from the command line on the board. However, converting this to standard C++:

# include <iostream>

int main(int argc, char * argv[]) {

std::cout << "Hello, Nios" << std::endl;

return 0;

}

does not work. The program compiles and links, but just sits at the beginning of execution, as though waiting for input. So, to try and resolve this I point my Rules.mak file to the standard C++ library, C:/altera/kits/nios2/bin/nios2-gnutools/H-i686-pc-cygwin/nios2-elf/lib/libstdc++.a. Now, the program doesn&#39;t hang, but rather dies with:

# hello_nios

BINFMT_FLAT: reloc outside program 0xa1b38 (0 - 0x5a314/0x51d10), killing hello_nios!

SIGSEGV

So now I&#39;m thinking this is the wrong library to link against for C++ -- is anyone out there successfully running C++ apps? If so, how do you link, include, etc? I see no real mention of C++ in the forum, although all the documentation says it&#39;s supported. I can include my Rules.mak and Makefiles if needed.

Any help would be greatly appreciated.

Ryan

8 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    hi rfrenz,

    The C++ library libstdc++.a coming with the nios2 compiler and toolchain is not built for uClinux. It is built against newlib, as opposed to uClibc. As a result, applications linked with it will not run on uClinux.

    If your C++ application does not need that library, you might be able to run it on uClinux. But in order to build it, you need to modify the Makefile by which your application will be built. The changes includes adding -mctors-in-init to LDFLAGS, and -lsupc++ to LDLIBS.

    for example, add the following to your makefile:

    LDFLAGS+= -mctors-in-init
    LDLIBS:=-lsupc++ $(LDLIBS)

    hope this helps,

    wentao
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi wentao,

    Thanks for the reply. I&#39;ve tried your suggestions and it has not fixed the problem. The executable still just sits as if waiting for input. I plan on writing my app from scratch, so as yet I don&#39;t need any special library files. However, I need to be able to write and compile Standard C++ code. All documentation I&#39;ve seen says this is in fact possible. See below for code and Makefile. I&#39;m using the default Rules.mak.

    // hello_nios.cpp# include <iostream>
    int main (int argc, char * argv) {
      std::cout << "Hello NIOS" << std::endl;
      return 0;
    }

    Makefile:

    include Rules.mak
    CFLAGS += -IC:/altera/kits/nios2/bin/nios2-gnutools/include/c++/3.4.1 -IC:/altera/kits/nios2/bin/nios2-gnutools/include/c++/3.4.1/nios2-elf
    LDFLAGS += -mctors-in-init
    LDLIBS := -lsupc++ $(LDLIBS)
    all: hello_nios.exe

    Thanks in advance,

    Ryan
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    unfortunately iostream is not available to uClinux (is it from libstdc++.a? better to check this). I should have pointed that out in the previous post.

    Currently the making of flat executable doesn&#39;t go through the final link, so unresolved symbols are not reported during that process. But the gdb file (.gdb) does go through the final link, so you can make the .gdb file at the same time to check if all symbols are resolved. If the generation of the gdb file fails, the flat file contains unresolved symbols, and will hang the system if you run it on uClinux.

    One of my C++ test on uClinux, function.cpp, is as follows,

    // overloading class member functions# include <stdio.h>
    //rectangle class declarartion
    class Rectangle
    {
    public:
      //constructors
      Rectangle(int width, int height);
      ~Rectangle(){}
      // overloaded class function DrawShape
     void DrawShape() const;
     void DrawShape (int aWidth, int AHeight) const;
    private:
     int itsWidth;
     int itsHeight;
    };
    //constructor implementation
    Rectangle::Rectangle(int width, int height)
    {
    itsWidth = width;
    itsHeight = height;
    }
    // overloaded DrawShape - takes no values
    // draws based on current class member values
    void Rectangle::DrawShape() const
    {
      DrawShape(itsWidth, itsHeight);
    }
    // overloaded DrawShape - takes Two Values
    // draws shape based on the parameters
    void Rectangle::DrawShape(int width, int height) const
    {
     for (int i = 0; i<height; i++)
     {
      for (int j = 0; j< width; j++)
      {
       printf( "*");
      }
      printf( "\n");
     }
    }
    // driver program to demonstrate overloaded functions
    int main()
    {
    //initialize the rectangle to 30,5
    Rectangle theRect(30,5);
    printf("DrawShape(): \n");
    theRect.DrawShape();
    printf("\nDrawShape(40,2): \n");
    theRect.DrawShape(40,2);
    return 0;
    }

    You can have a try,

    regards,

    wentao
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    You might run into an error like

    In function `__gnu_cxx::__verbose_terminate_handler()&#39;:
    /build/nios2/bin/nios2-gnutools/src/gcc/libstdc++-v3/libsupc++/vterminate.cc:68:
     undefined reference to `_impure_ptr&#39;

    The reason for this error is that libsupc++ from the toolchain was built with newlib header files, which define stderr as _impure_ptr->stderr. A possible workaround is to put a dummy _impure_ptr somewhere in the application:

    #include <stdio.h>
    struct __myreent
    {
      int _errno;
      FILE *_stdin, *_stdout, *_stderr;
    };
    static struct __myreent __myblock = {
     0, stdin, stdout, stderr
    };
    struct __myreent *_impure_ptr=&__myblock;
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thanks a lot, Wentao, that worked.

    I&#39;m not sure if anyone really cares, but there is no mention in the Microtronix documentation that libstdc++ is not supported. This is a pretty big deal. The fact that large chunks of the C++ language are not supported should be explicitly noted. You pay for what you get I guess.

    Anyway, thanks a lot Wentao. At least I can write Object-Oriented C code. Better than nothing.

    Ryan
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    uClibc has supported libstdc++since v0.9.11, and I&#39;ve used it without problem under m68k uClinux.

    EDIT: Perhaps I should&#39;ve clarified.... you have to re-build libstdc++ against uClibc yourself. It&#39;s not difficult. http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/smile.gif Also, some quick googling turned up another alternative as well: http://cxx.uclibc.org/ (http://cxx.uclibc.org/) which is a small libstdc++ replacement. (incomplete, but seems to be together enough for the basics, like streams) I&#39;ll actually probably end up building libstdc++ for nios-II soon myself, so I&#39;ll let you know if there&#39;s any caveats.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hello,

    We have had very occasional requests for C++ support in the past, so we have only supported it to the level customers have required until now.

    However, we are planning on integrating uClibc++ support once it is a little more mature (it is currently in the Alpha stage of development).

    Regards,

    Robert

    Microtronix
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thanks Nate and Robert,

    <div class='quotetop'>QUOTE </div>

    --- Quote Start ---

    you have to re-build libstdc++ against uClibc yourself. It&#39;s not difficult. Also, some quick googling turned up another alternative as well: http://cxx.uclibc.org/ (http://cxx.uclibc.org/) which is a small libstdc++ replacement. (incomplete, but seems to be together enough for the basics, like streams) I&#39;ll actually probably end up building libstdc++ for nios-II soon myself, so I&#39;ll let you know if there&#39;s any caveats.[/b]

    --- Quote End ---

    I&#39;m going to try this too. I&#39;ll post here with success or failure.

    Thanks again all,

    Ryan