c++makefileexe

One-file Makefile Problems: C++


I've been fiddling with a makefile for the past hour and I can't seem to get it. I need a makefile that will compile main.cpp in /source/ and place the result (e2.exe) in the /build/ folder. Here is what I have right now:

build: source/main.cpp
    make -C build/ source/main.cpp

I have no idea why it's not working. I have tried soo many things like

build: source/main.cpp
    make -o build/ source/main.cpp

build: source/main.cpp
    make -C e2.exe source/main.cpp

build: source/main.cpp
    make -C source/main.cpp e2.exe

build: source/main.cpp
    make source/main.cpp -o e2.exe

And lots of other combinations. Here's my folder structure:

root/
╚build/
 source/
  ╚main.cpp
 e2.pnproj
 e2.pnps
 Makefile

The reason for all the attempts is that nearly every tutorial I come across has complex examples and has everything in a different order.

I think the closest I have come to it working was this:

build: source/main.cpp
    make -C build/ source/main.cpp

And the error is:

make -C build/ source/main.cpp
make[1]: Entering directory `/c/WiiGames/e2/build'
make[1]: *** No rule to make target `source/main.cpp'.  Stop.
make[1]: Leaving directory `/c/WiiGames/e2/build'
"make": *** [build] Error 2

Solution

  • First off, I notice you're makeing everything. At some point, you do have to cc (or gcc, or whatever) it. :)

    I'd think you could just do like

    build: source/main.cpp
            $(CXX) -o build/e2.exe source/main.cpp
    

    where $(CXX) is the name/path to your compiler. (Note, if you copy/replace, replace those eight spaces with a tab.)