Link to home
Start Free TrialLog in
Avatar of Alan Huseyin Kayahan
Alan Huseyin KayahanFlag for Sweden

asked on

external functions

hi
i am trying to write a program which runs dos commands like copy,dir. And i have source files copy.c and dir.c which include copy function and dir function.
in my main function :
main.c
#include <stdio.h>
int main(){
   int num;
   scanf("%d",num);
   switch(num){
       case 1:
           #include copy.c
           printf("result:%d",copy());
        break;
       case 2:
           #include dir.c
           printf("result:%d",dir());
        break;
   }
}
including each file is rational? (defining each function in different source files is indispensible for my project.. )
i tried defining functions as externals instead of including each source file.But i have problems while linking them togehter because there is a lot of source files... any easy way to link them in linux environment?
how can i implement this code without including each source file?
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi Husy,

You can't call a system function that way.  Sorry.

But you can call system functions.  Check out the system() API.  It should do most everything that you want.


  system ("copy file1 fileb");

And if you need to read the commands output, or redirect it, check out popen().



Good Luck,
Kent
Avatar of phoffric
phoffric

Here's my interpretation of your question. You have a number of files, such as copy.c, dir.c which have functions in them that behave like the corresponding DOS commands, copy, dir, etc. So, for example, when you call dir(), I'm guessing that dir() prints the directory listing of the current directory to stdout. How it does it, whether using the system() call as just suggested, or other approaches is hidden from the caller.

(By the way, you refer to copy(), which is surprising, since I would expect two arguments, "source" and "destination".)

OK, to do this, the standard approach is to create a single header file, let's call it "myDosEmulation.h" add following line:
#include "myDosEmulation.h" right after your #include <stdio.h>.

The myDosEmulation.h file has in it all the declarations of the public functions that are in your set of .c files.
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alan Huseyin Kayahan

ASKER

thx for answers so there is no way other than including all files.
>> thx for answers so there is no way other than including all files
Well, you could build a static library out of copy.c and dir.c but in the end, one way or another, the object code has to be linked in. The other option is a dynamic library but I think that's probably beyond the scope of the question.