0

So I have these three files:

Linking.cpp:

#include "Liking.h"

int main()
{
    print();
}

Liking.h (I missed the "h" in the names of the files and I'm afraid to change it and got some problems):

#pragma once

void print();

liking2.cpp:

#include <iostream>
#include "Liking.h"
#include <string>


void print()
{
    std::string name;
    std::cout << "Insert your name: ";
    std::cin >> name;
    std::cout << "Your name is: " << name<< std::endl;
}

Now my question is about using makefile in order to link these .cpp files so I made one and it is this:

Linking: Linking.o liking2.o
    g++ Linking.o liking2.o -o Linking

Linking.o: Linking.cpp
    g++ -c Linking.cpp

Linking.o: liking2.cpp
    g++ -c liking2.cpp

clean:
    rm *.o Linking

But the problem comes (at least this is what I'm thinking I'm missing) when I try to use this beacuse I don't have any clues. The videos I've watched on youtube showed the "make" word but it doens't seem to work. I've searched a lot and I found the "mingw32-make.exe" but when I open the make file in my Visual Studio code (By the way I'm also using windows) and use this command in the terminal I get this problem:

makefile:8: warning: overriding recipe for target 'Linking.o'
makefile:5: warning: ignoring old recipe for target 'Linking.o'
g++ -c liking2.cpp
g++ Linking.o liking2.o -o Linking
g++: error: Linking.o: No such file or directory
makefile:2: recipe for target 'Linking' failed
mingw32-make: *** [Linking] Error 1

Now I know that this kind of question is asked a lot (I know because I've searched) but the only three question I saw that could clarify had all problems so they didn't help me at all:

Makefile --mingw32-make.exe mingw32-make.exe : *** missing separator. Stop mingw32-make.exe: *** [All] Error 2 - Codelite

If you guys could at least put me on the right path about what I'm missing that would be really helpful

Isaias
  • 43
  • 1
  • 5
  • I see two rules for Linking.o – user4581301 Feb 09 '22 at 22:37
  • 2
    Got some typos in there, I think. But I think `g++ -c liking2.cpp` will emit liking2.o rather than Linking.o. – user4581301 Feb 09 '22 at 22:40
  • Yeah, I fixed the problem but I'm still getting the error message – Isaias Feb 09 '22 at 22:58
  • 1
    The makefile is wrong, you should not have 2 recipes for the same target. (You can have multiple dependency lists but not multiple recipes) – M.M Feb 09 '22 at 22:58
  • 2
    "Linking.o: No such file or directory" happens because you didn't have `-o Linking.o` telling it to create an output file of that name. similar problem will happen for liking2 – M.M Feb 09 '22 at 23:00
  • &M.M are you referring to the fourth and the seventh line? – Isaias Feb 09 '22 at 23:17
  • 1
    This one: `g++ -c liking2.cpp` -> `g++ -c liking2.cpp -o Linking.o`. If you do not specify a particular output, the compiler will default to generating a name that matches the input file, liking2 in the case with the .o extension. – user4581301 Feb 09 '22 at 23:49
  • @user4581301 But why would I give it that name? Shouldn't I use 'g++ -c liking2.cpp -o Liking2.o' instead? I also made the mistake of giving two recipes to the same target, but I've already fixed that, if you're referrring to this. – Isaias Feb 10 '22 at 00:06
  • 2
    No one has answered the question yet, so I recommend updating the question with your current makefile and current errors so we can see where you are at. If you've fixed the stuff you're saying you've fixed, you're getting different errors and we're just sewing confusion trying to help you solve the old ones. – user4581301 Feb 10 '22 at 00:15
  • 1
    `rm` isn't a windows command . Unless you are running under some unix-like shell program that offers a `rm` command, you should see a matching error message for the attempt to execute `make clean`. Also the executable might have `.exe` extension even though you didn't explicitly ask for it – M.M Feb 10 '22 at 00:41
  • 2
    @user4581301 some ports of gcc to windows have `.obj` as the default extension if you don't use `-o` , so I would recommend always explicitly specifying – M.M Feb 10 '22 at 00:41
  • 1
    Please add the new error messages. What you have looks like it should have solved all of the old ones. – user4581301 Feb 10 '22 at 16:30
  • @user4581301 Actually when I updated the question I said it was working already thanks to your answers. Should I close the topic? – Isaias Feb 10 '22 at 19:07
  • 1
    Prefer to self answer. Move the fixed code to an Answer and delete it from the question, then explain what was wrong and how the changes to the makefile solved the problem. This helps future askers with a similar problem and you might gain a few reputation points (and corresponding site privileges) from it. – user4581301 Feb 10 '22 at 19:11

1 Answers1

0

Thanks to the explanations in the comments and also following their advice I'll post here the fixed code and I'll explain the differences:

Linking: Linking.o liking2.o
    g++ -o Linking Linking.o liking2.o

Linking.o: Linking.cpp
    g++ -c Linking.cpp 

liking2.o: liking2.cpp
    g++ -c liking2.cpp

clean:
    rm *.o Linking

Now taking the answer from the question: makefile error no such file or directory I changed the second line of code where I moved the -o Linking to the beginning and also, as it was pointed out in the comments, I did a mistake in the seventh line where I used two recipes to the same target, don't do this with your makefile as well. These changes that I did were enough to make the makefile work and I hope that this answer will be able to help other people as well in the future and thank you for all for the help.

Isaias
  • 43
  • 1
  • 5