1

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?

Bittu
  • 33
  • 4
  • The problems are with the spaces in the path. Either escape them or get rid of them in the actual path. – Marcell Juhász Oct 15 '21 at 16:58
  • 2
    Or try taking them in double quotes – Eugene Sh. Oct 15 '21 at 17:00
  • when I am executing `gcc -I"C:\Users\Bittu\OneDrive - Intel\Documents\My Documents\" factorial.o Prog1.o -o Prog1`, it is giving `gcc: fatal error: no input files compilation terminated.` – Bittu Oct 15 '21 at 17:15
  • 1
    You have to escape the \, too. Escape by adding another \ in front of them – frippe Oct 15 '21 at 17:47
  • when I am executing `gcc -I"C:\Users\Bittu\OneDrive - Intel\Documents\My Documents" factorial.o Prog1.o -o Prog1`, it is giving `gcc: error: factorial.o: No such file or directory` – Bittu Oct 16 '21 at 04:59

0 Answers0