Combining two programs into one in C

  • Thread starter Thread starter kthouz
  • Start date Start date
  • Tags Tags
    Programs
Click For Summary

Discussion Overview

The discussion revolves around the possibility and methods of combining two or more C programs into a single executable. Participants explore various approaches to achieve this, including using preprocessor directives and compiling multiple source files together.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant inquires about combining two C programs without rewriting existing code, seeking a method to link to previously written programs stored on disk.
  • Another participant suggests using the #include preprocessor directive to include one source file in another, and mentions system calls like exec() for running compiled programs from within another.
  • A different participant proposes writing a single main() function and compiling multiple source files together, providing an example of how to structure the code and compile it on UNIX/Linux systems.
  • There is a suggestion to create a library or archive of functions, noting that the naming conventions and creation methods depend on the operating system and compiler used.
  • One participant expresses confusion about the relevance of the string library to the topic and requests resources for learning about it.
  • Another participant provides links to resources that list functions in the string.h library, although the connection to the main topic is questioned.

Areas of Agreement / Disagreement

Participants present multiple approaches to combining programs, with no consensus on a single method. Some express confusion about the relevance of certain topics, indicating a lack of agreement on the focus of the discussion.

Contextual Notes

Some participants may have varying levels of understanding of C programming concepts, which affects the clarity of the discussion. There are also unresolved questions regarding the application of specific libraries and functions.

Who May Find This Useful

This discussion may be useful for C programmers looking to understand how to combine multiple source files into a single program, as well as those seeking resources for learning about specific libraries like 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
 

Similar threads

Replies
86
Views
3K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
16
Views
3K
Replies
6
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 16 ·
Replies
16
Views
4K