Elephant
Elephant

Reputation: 29

How to create a working makefile for a .cpp file

I'm trying to figure out how to get a makefile working for my .cpp file

Here is the code for main.cpp:

#include <iostream>

using namespace std;

int main() {
    cout << "hello world!" << endl;
   return 0;
}

Here is my code for the make file: all: main.exe

main.exe: main.o
     g++ -o main.exe main.o

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

When I enter the command "make"

I get this: enter image description here

But when I run main.exe I get this: enter image description here

And if I run main.exe as administrator I get this: enter image description here

Upvotes: 0

Views: 256

Answers (1)

Cameron Bielstein
Cameron Bielstein

Reputation: 429

Your makefile looks fine. However, it looks like you're compiling on a Linux machine through Putty. If that's true, there's your problem. Running g++ on Linux will target Linux. Simply appending .exe to your file name will not make it compatible with Windows.

There are tutorials out there for compiling for Windows on Linux, or you can install the proper compilers on your Windows machine, or simply run the compiled program inside a Linux environment.

Upvotes: 2

Related Questions