Combining two programs into one in C

  • Thread starter Thread starter kthouz
  • Start date Start date
  • Tags Tags
    Programs
Click For Summary
Combining multiple C programs is feasible by using the #include preprocessor directive to incorporate one source file into another. Additionally, system calls like exec() can execute a compiled program from another. A common approach is to compile multiple source files together, ensuring they do not all contain a main() function. For example, a main module can call functions defined in other modules. In UNIX/Linux, this can be done using a command like `cc -o myprog prog1.c prog2.c prog3.c`. Alternatively, creating a library or archive of functions is possible, with common file types like .sl, .so, or .a used in UNIX environments. For those seeking to understand string manipulation in C, resources were suggested, including links to comprehensive lists of string functions available in string.h.
kthouz
Messages
188
Reaction score
0
Hi!
Is it possible to combine two programs in C?
I mean for example you're wriitng a program where you will need to introduce another one that you have written before and you don't need to wrest your time in rewritting it again. Then i want to know how i can create if possible a link which can let you access it where it saved on the disk. (Like those links on the web pages).
 
Technology news on Phys.org
You can use the #include preprocessor directive to include one source file inside of another. Depending upon your operating system, you can also use system calls like exec() to run a compiled program from within another.

- Warren
 
You can also write one "main()" module, then compile all three together. Assuming the other two modules are not just one giant main() each. example
Code:
// prog1.c
int foo1(int);
int foo2(int);
int main(int argc, char **argv)
{
      printf("%d\n", foo1(13) +  foo2(10));
      return 0;
}
Code:
// prog2.c
 int foo1(int val)
{
    return val+1;
}
Code:
// prog3.c
int foo2(int val)
{
    return val+val;
}

Depending on your OS and your development environment you cimpile these togetehr. Here is a UNIX/Linux example:
Code:
cc -o myprog  prog1.c prog2.c prog3.c

You can also create a library or an archive of your foo1, foo2 modules. What they are called and how to make them depends on your OS and your compiler: .sl .so .a
are common "filetypes" for these in UNIX.
 
Ok i see but i can't understand clearly. May be it is just because i still on the low level. so can you give me a link where i can learn about string library cause i guess it is that field!?
 
I don't see how string.h deals with anything in this thread :confused:
But:
A lot of the string functions start with "str" - here is a short list:
http://www.cppreference.com/all_c_functions.html

This lists all of the functions in string.h:
http://www.utas.edu.au/infosys/info/documentation/C/CStdLib.html#string.h
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
6
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 1 ·
Replies
1
Views
782
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 14 ·
Replies
14
Views
3K
Replies
50
Views
4K