Shop OBEX P1 Docs P2 Docs Learn Events
Propeller C Function calls for other files — Parallax Forums

Propeller C Function calls for other files

MJFMJF Posts: 19
edited 2013-09-29 12:07 in Learn with BlocklyProp
Can someone please explain how I can call a function from one file to another. I want to produce a library of reusable code in different files ( .C files) and call from my main program, is this the correct way of reusing my code if so how is it achieved.

Also can I create my own header files and how would these differ from .C files.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-09-24 11:29
    Yes, you can create your own header files. This is all standard C, not at all Propeller specific. By convention, a header file just contains declarations ... of types, constants, function / procedure headers, common variables, etc. By convention, .C files contain #includes for header files and the function / procedure bodies themselves and variables local to that .C file, but global to the functions / procedures contained within.

    You call a function in another .C file by including a header for the function in a header (.H) file for that .C file and including that header in the other .C file that contains the call. Essentially, you're creating a library file which normally consists of a header (.H) file that contains the definitions and a program (.C) file that contains the functions. The header file gets #include'd anywhere the function is to be used (including within the library program file). Usually the library (.C) file gets compiled into an object file which is what gets linked with your program rather than compiling it every time.
  • jazzedjazzed Posts: 11,803
    edited 2013-09-24 12:05
    Page 29 of the SimpleIDE 0-9-43 user guide tells how to make a library for SimpleIDE use.
    SimpleIDE will attempt to automatically include libraries found in the Learn/Simple Libraries folder.

    Libraries should be made in their own folders or the Learn/Simple Libraries folder, and called by other projects in totally separate, unrelated folders. Test code for libraries should be put in the main.c file which will not be included in the final library.
  • MJFMJF Posts: 19
    edited 2013-09-29 03:06
    I can now see how the header file is added, and if I put a function in the header file I can call it form the main.c file. However if I have a separate file with my function in this does not seem to work. Does anyone have an example piece of code of how I reference the new.c function file from the header file and then call the function from the main.c file.

    I'm sure this is basic stuff but I am new to C and trying to work my way through it.
  • jazzedjazzed Posts: 11,803
    edited 2013-09-29 11:01
    MJF wrote: »
    I can now see how the header file is added, and if I put a function in the header file I can call it form the main.c file. However if I have a separate file with my function in this does not seem to work. Does anyone have an example piece of code of how I reference the new.c function file from the header file and then call the function from the main.c file.

    I'm sure this is basic stuff but I am new to C and trying to work my way through it.

    Hi,

    To add a function in a .c file to be used in the program, the .c file must be added in the project manager.
  • MJFMJF Posts: 19
    edited 2013-09-29 11:20
    This is what I have done

    This is my main.c code

    #include "simpletools.h" // Include simple tools
    #include "Header.h"


    int stop=0;
    //int Edgeneg();




    int main() // Main function
    {

    while(stop==0)
    {
    int n=Edgepos(1);
    printf("xxx %d",n);
    stop=1;

    }
    }


    This is my Header Code

    #include"simpletools.h"



    This is my function code

    #include"simpletools.h"
    #include"Header.h"






    int Edgepos(int Edge_Val)
    {

    printf(" Function Input made----");
    Latch=22; //---- just used to check return value

    return (Latch);

    }


    And this is the error I get, if I put the function in the header I return the value and the function works.

    Project Directory: /Users/markfisher/Documents/SimpleIDE/Marks Projects/Header2/Main1/


    propeller-elf-gcc -v GCC 4.6.1 (propellergcc_v1_0_0_2097)
    propeller-elf-gcc -I . -L . -I /Users/markfisher/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools -L /Users/markfisher/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools/cmm/ -I /Users/markfisher/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libsimpletext -L /Users/markfisher/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libsimpletext/cmm/ -I /Users/markfisher/Documents/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c -L /Users/markfisher/Documents/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c/cmm/ -Os -mcmm -m32bit-doubles -fno-exceptions -std=c99 -c Fun1.c -o cmm/Fun1.o
    propeller-elf-ar rs cmm/Main1.a cmm/Fun1.o
    /opt/parallax/bin/propeller-elf-ar: creating cmm/Main1.a
    propeller-elf-gcc -I . -L . -I /Users/markfisher/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools -L /Users/markfisher/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools/cmm/ -I /Users/markfisher/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libsimpletext -L /Users/markfisher/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libsimpletext/cmm/ -I /Users/markfisher/Documents/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c -L /Users/markfisher/Documents/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c/cmm/ -o cmm/Main1.elf -Os -mcmm -m32bit-doubles -fno-exceptions -std=c99 Main1.c cmm/Main1.a -lm -lsimpletools -lsimpletext -lsimplei2c -lm -lsimpletools -lsimpletext -lm -lsimpletools -lm
    Main1.c: In function 'main':
    Main1.c:14:5: warning: implicit declaration of function 'Edgepos' [-Wimplicit-function-declaration]
    /var/folders/2g/tj8fdg2923q7cv03jjjphvj40000gn/T//cclgJHoJ.o: In function `_main':
    (.text+0xe): undefined reference to `_Edgepos'
    collect2: ld returned 1 exit status
    Done. Build Failed!


    Check source for bad function call or global variable name `_Edgepos'
  • jazzedjazzed Posts: 11,803
    edited 2013-09-29 11:35
    Can you zip the project and post it here?
  • MJFMJF Posts: 19
    edited 2013-09-29 11:49
    Please find attached zipped file.
  • jazzedjazzed Posts: 11,803
    edited 2013-09-29 11:58
    Here's the simplest compile-able version.
    It does not use a header file or extern declaration for your function, and gives a warning.


    Btw, this syntax should be avoided in C:

    Trigger,Trig_Latch=Edgepos(Input_1);
  • MJFMJF Posts: 19
    edited 2013-09-29 12:07
    Thanks Jazzed.
Sign In or Register to comment.