Learn how to link two C files

Image for post
Image for post

Have you ever wondered how to link multiple source files in C ? Well wonder no more, I will show you all easy steps to link your own C-Program source files.

Step 1: Create Your Two C-Program Source Files

First thing to do is create your two programs. Here I will name one file as “File1.c” and the other “File2.c”. File1.c will contain our main function, and call our hello() function. File2.c will contain the actual function definition, which provides the actual body of the function.

int main(void) {
hello();
return 0;
}
#include <stdio.h>

void hello(void) {
printf("Hello, World!\n");
}

Step 2: Save Both Files In The Same Location

Really you could put them in different locations, but you have to remember both locations. In this article I will save both files on my Windows Desktop.

C:\Users\<user_name>\Desktop

Step 3: Open Command Prompt And Run These Commands

Now, open your command prompt.

A) Navigate to the location where you stored your files:
You can achieve this by using the change directory command “cd”. I placed my files on the Desktop so the command I will use is:

cd Desktop

B)Run the C-compiler gcc:
This will compile our C-programs. We will also create the executable file (called combined) that will combine these our two C-programs. Just run the following command. Make sure you are in the same directory as your source files.

gcc file1.c file2.c -o combined

C) Run Your Executable File:
While still in your command prompt and in the same directory simply type the name of the executable (combined), and the program will run.

combined

Step 4: You’re Done !

Here is a video showing the steps in this article.

Notice: Your program should now have been executed, my program printed “Hello, World!” to the command prompt !

Thanks for reading this article I hope its helpful to you all ! Keep up the learning, and if you would like more computer science, programming and algorithm analysis videos please visit and subscribe to my YouTube channels (randerson112358 & compsci112358 )

Check Out the following for content / videos on Computer Science, Algorithm Analysis, Programming and Logic:

YouTube Channel:
randerson112358: https://www.youtube.com/channel/UCaV_0qp2NZd319K4_K8Z5SQ

compsci112358:
https://www.youtube.com/channel/UCbmb5IoBtHZTpYZCDBOC1CA

Website:
http://everythingcomputerscience.com/

Video Tutorials on Recurrence Relation:
https://www.udemy.com/recurrence-relation-made-easy/

Video Tutorial on Algorithm Analysis:
https://www.udemy.com/algorithm-analysis/

Twitter:
https://twitter.com/CsEverything

YouTube Channel:

Image for post
Image for post

Computer Science Website:

Image for post
Image for post

Udemy Videos on Algortithm Analysis:

Image for post
Image for post

Resources:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store