external functions

Posted on 2010-01-12
Medium Priority
Last Modified: 2012-05-08
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?
Question by:Alan Huseyin Kayahan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 45

Expert Comment

by:Kent Olsen
ID: 26295526
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,
LVL 32

Expert Comment

ID: 26295656
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.
LVL 40

Accepted Solution

evilrix earned 2000 total points
ID: 26295942
>> 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
LVL 29

Author Comment

by:Alan Huseyin Kayahan
ID: 26301415
thx for answers so there is no way other than including all files.
LVL 40

Expert Comment

ID: 26301663
>> 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question