Forum Discussion

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

C++ Nios Program

Hello,

Can anyone point me to an example of actually building a Nios-II project with cpp? I have tried many ways and it always comes down to a build error. The problem seems to be related to the alt_main.c. There must be a simple method of creating a CPP program as the documentation states everywhere that CPP is supported. I cannot find a template or an example on the website. I see where the simple socket server has a cpp implementation but it is not a full example showing the linkage with a BSP.

Thanks,

4 Replies

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

    First of all you should rename as alt_main.cpp.

    Then, if your .cpp contain functions which are supposed to be called from .c sources, you must declare them as extern "C"
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thanks for the reply, but I have tried that before and run into bsp build errors with it not being able to find the alt_main.c file even thought I think (???) I changed the correct makefile to build a the cpp file.

    Just one zipped up example of a complete project and bsp would be great if anyone has one.

    It just seems like it should be a no-brainer, but I just keep going around in circles.

    Thanks again.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Try with others known extensions like .cc .C

    I've heard of this problem
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    After much trial and error I have found a solution to building a simple CPP project.

    1. Build the BSP as usual (I make sure to check the enable small lib and small drivers).

    2. Build a NIOS-II project based on the BSP.

    3. With the project selected, add a new file to the project. I used the "File from Template". Make sure it has the .cpp extension. Now you should have the file insterted in your project, but it will have no code inside.

    4. Open the file for editing and insert the standard main() function, I used:

    extern "C"

    {

    #include <stdio.h>

    #include "system.h"

    }

    int main(void)

    {

    puts("Hello World!!!");

    return 0;

    }

    Life is good again!:)