?
Solved

external functions

Posted on 2010-01-12
5
Medium Priority
?
298 Views
Last Modified: 2012-05-08
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?
0
Comment
Question by:Alan Huseyin Kayahan
[X]
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
5 Comments
 
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,
Kent
0
 
LVL 32

Expert Comment

by:phoffric
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.
0
 
LVL 40

Accepted Solution

by:
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
0
 
LVL 29

Author Comment

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

Expert Comment

by:evilrix
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.
0

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