Search code examples
ceclipseintel-fpgabare-metal

Can we include C source file in another way?


I am doing a bare metal project on Cyclone V and now I am trying to make a bare metal application in C. However, I have some issue in including source file.

For example, I use the function alt_fpga_state_get() from alt_fpga_manager.h but it gives me error message

"undefined reference to 'atl_fpga_state_get()'

Knowing that it needs a source file containing the function, I import the source file with no problem. However, here come another thing that inside alt_fpga_manager.c it also gives an error message

"undefined reference to 'alt_clock_is_enable'

Then I have to import source file for alt_clock_manager.h and the problem keep going on like that until I end up import the whole src folder. After that all the "undefined reference" problem are solved but it come with another problem telling me that my OCRAM is overflowed ( I think because of adding many source file ).

I would like to know if there is any solution for this because keep importing source file is not a convenient way to do. I did look at some examples and I found that in their makefile having this line

HWLIBS_SRC  := alt_reset_manager.c alt_clock_manager.c alt_spi.c alt_globaltmr.c alt_timers.c alt_watchdog.c 

I think it is the way they include the source file but I am not sure. Hope someone can give me a clue to solve this problem.

Thanks !


Solution

  • when you #include an header file you get the function declarations (etc depending on header file contents), that does not define them, you missed to link with needed object or lib files

    Example :

    main.c is

    #include "f.h"
    
    int main()
    {
       f();
    }
    

    f.h is

    extern void f();
    

    f.c is

    void f() {}
    

    If I just consider main.c :

    /tmp % gcc main.c 
    /tmp/ccFs7Gyz.o: In function `main':
    main.c:(.text+0xa): undefined reference to `f'
    collect2: ld returned 1 exit status
    

    But also considering f.c :

    /tmp % gcc main.c f.c
    

    Of course can also do in several steps

    /tmp % gcc -c f.c
    /tmp % gcc main.c f.o
    

    etc

    P.S. do not #include a source file, so no #include "f.c" in mainc.c