Propeller C Function calls for other files
MJF
Posts: 19
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.
Also can I create my own header files and how would these differ from .C files.
Comments
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.
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.
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.
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'
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);