• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

external functions

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 :
#include <stdio.h>
int main(){
   int num;
       case 1:
           #include copy.c
       case 2:
           #include dir.c
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?
Alan Huseyin Kayahan
Alan Huseyin Kayahan
1 Solution
Kent OlsenData Warehouse Architect / DBACommented:
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,
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.
evilrixSenior Software Engineer (Avast)Commented:
>> i have problems while linking them togehter because there is a lot of source files
You need to tell your compiler to compile the copy.c and dir.c as part of your executable. If you are using linux I assume you are using gcc? If so, something like this will do it...

gcc -o myexe main.c copy.c dir.c

That will create an executable called "myexe"

As phoffric has eluded, you also need to provide forward declarations for these functions. Do you have a dir.h and copy.h? If so just include them at the top of you main.c file. If not you can add the forward declarations yourself before the main function in the main.c files.

Note: I assume that your main function is in a source file called main.c
Alan Huseyin KayahanAuthor Commented:
thx for answers so there is no way other than including all files.
evilrixSenior Software Engineer (Avast)Commented:
>> 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.

Featured Post

IT Degree with Certifications Included

Aspire to become a network administrator, network security analyst, or computer and information systems manager? Make the most of your experience as an IT professional by earning your B.S. in Network Operations and Security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now