| | |
Need help understanding external functions
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2007
Posts: 49
Reputation:
Solved Threads: 0
Can someone please explain to me and show a sample of using external function?
Let say I have my main file: main.c
and other file only with function: print.c
I know I should prototype print function in my main.c, define it in print.c and call it from main.c. (at least I think that's how it is)
when I do it, the compiler refuses to work, that's why I need some sample which I could not find anywhere (ebooks, google, textbook) - really weird.
Thanks in advance
Let say I have my main file: main.c
and other file only with function: print.c
I know I should prototype print function in my main.c, define it in print.c and call it from main.c. (at least I think that's how it is)
when I do it, the compiler refuses to work, that's why I need some sample which I could not find anywhere (ebooks, google, textbook) - really weird.
Thanks in advance
C Syntax (Toggle Plain Text)
/* main.c */ extern void foo ( void ); int main ( void ) { foo(); return 0; }
C Syntax (Toggle Plain Text)
/* print.c */ #include <stdio.h> void foo ( void ) { puts ( "foo!" ); }
I'm here to prove you wrong.
You likely don't link your my.c anyhow. It depends on your compiler, like gcc main.c my.c -o main both compiles the modules, and links them, producing the executable main. This is similar for all compilers, but the problem is when you use some graphical ide, which is bad anyhow, but if you do, you must add your my.c in project, so that the ide knows what files need to be linked. Usually, the function prototypes and global variables defined in one file,would be declared not in that file, but in include file, like my.h, which would be included to both files, with #include <my.h>. It works also otherwise, but this is a good habbit, so that you don't always have to write your declarations separately in every file. If you are very lazy, your program would certainly compile if you would include your c file to the main file, with #include <my.c>, but it's not good to be that lazy.
![]() |
Other Threads in the C Forum
- Previous Thread: Reversing a string
- Next Thread: little formatting question
| Thread Tools | Search this Thread |
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string strings structures suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






