Based on the hints I have got from this thread and the comments below
I have reached upto the following points:
Let's say I have a c file named factorial.c
in C:\Users\Bittu\OneDrive - Intel\Documents\My Documents\
directory
#include "factorial.h"
int fact(int num)
{
if(num==1)
return num;
else
return num*(fact(num-1));
}
and factorial.h
in the same directory
int fact(int num);
and Prog1.c
in C:\Users\Bittu\OneDrive - Intel\Documents\My Documents\Programming\
directory
#include <stdio.h>
#include "factorial.h"
int main()
{
int n;
printf("Enter the no.:");
scanf("%d",&n);
printf("Factorial of %d = %d",n,fact(n));
return 0;
}
Now, in order to compile Prog1.c
:
Path: C:\Users\Bittu\OneDrive - Intel\Documents\My Documents\Programming\
Command: gcc -c Prog1.c -I"C:\Users\Bittu\OneDrive - Intel\Documents\My Documents"
In order to compile factorial.c
:
Path: C:\Users\Bittu\OneDrive - Intel\Documents\My Documents\
Command: gcc -c factorial.c
but, when I am trying to link factorial.o
and Prog1.o
to produce a final Prog1.exe
file, I am facing errors.
Path: C:\Users\Bittu\OneDrive - Intel\Documents\My Documents\Programming\
Command: gcc Prog1.o factorial.o -o Prog1 -L"C:\Users\Bittu\OneDrive - Intel\Documents\My Documents"
Error:
gcc: error: factorial.o: No such file or directory
one solution I found is using the complete path in factorial.o
, i.e:
gcc Prog1.o "C:\Users\Bittu\OneDrive - Intel\Documents\My Documents\factorial.o" -o Prog1
or,
gcc Prog1.o .\..\factorial.o -o Prog1
But, this would not be a good solution, as we might have a c file (in C:\Users\Bittu\OneDrive - Intel\Documents\My Documents\Programming\
) which is linking to many object files (present in C:\Users\Bittu\OneDrive - Intel\Documents\My Documents\
) and as a result our executing command will become very long;
so is there any other solution to link object files from another directory?