Search code examples
windowspathmakefilerelative-pathmsys2

Makefile only working with relative paths on MSYS2 (WIndows)


I have a set of programs that compile fine on Ubuntu and I'm working on a setup to compile on Windows as well. I'm using MSYS2.

There is a main Makefile which looks like this:

#### Project main directory

DIR_MAIN=$(shell pwd)

#### Compile all

all:
    @$(MAKE) -C src/Misc DIR_MAIN=$(DIR_MAIN)
    @$(MAKE) -C src/CF_states DIR_MAIN=$(DIR_MAIN)

etcetera. In src/ there are folders each who contain a specific Makefile for that project. They have the following structure:

#### Directories and flags

ifndef $(DIR_MAIN)
  DIR_MAIN=../..
endif

DIR_EXE=$(DIR_MAIN)/Programs
DIR_SRC=$(DIR_MAIN)/src/Misc
DIR_SFMT_SRC=$(DIR_MAIN)/src/sfmt
DIR_BLD=$(DIR_MAIN)/build/Misc
COMP=g++

COMPILE_FLAGS= -std=c++11 -O3 -lstdc++ -msse2
LINK_FLAGS= -O3 -fopenmp -lgsl -lgslcblas -lm -lhdf5_cpp -lhdf5


#### Compile all

all: setup $(DIR_BLD)/input.o $(DIR_BLD)/misc_sphere.o $(DIR_BLD)/misc.o 

setup:
    @mkdir -p $(DIR_BLD)
    @mkdir -p $(DIR_EXE)

#### Misc source

$(DIR_BLD)/input.o: $(DIR_SRC)/input.cpp $(DIR_SRC)/input.h $(DIR_SRC)/misc_sphere.h
    ${COMP} -c input.cpp ${COMPILE_FLAGS} -o $@

$(DIR_BLD)/misc_sphere.o: $(DIR_SRC)/misc_sphere.cpp $(DIR_SRC)/misc_sphere.h $(DIR_SRC)/misc.h
    ${COMP} -c misc_sphere.cpp ${COMPILE_FLAGS} -o $@

$(DIR_BLD)/misc.o: $(DIR_SRC)/misc.cpp $(DIR_SRC)/misc.h
    ${COMP} -c misc.cpp ${COMPILE_FLAGS} -o $@

In this way the specific Makefiles can be run from their folder in src/, using relative paths, or from the main Makefile, using absolute paths.

This works on Ubuntu but on MSYS2 only the relative paths work, i.e. running each of the specific Makefiles from their folders. When I run the main Makefile using

mingw32-make.exe

I get the error

$ mingw32-make.exe
mingw32-make[1]: Entering directory 'C:/msys64/home/Jorgen/fqhe_mc_sphere/src/Misc'
mingw32-make[1]: *** No rule to make target '/home/Jorgen/fqhe_mc_sphere/src/Misc/input.cpp', needed by '/home/Jorgen/fqhe_mc_sphere/build/Misc/input.o'.  Stop.
mingw32-make[1]: Leaving directory 'C:/msys64/home/Jorgen/fqhe_mc_sphere/src/Misc'
Makefile:8: recipe for target 'all' failed
mingw32-make: *** [all] Error 2

even though the file /home/Jorgen/fqhe_mc_sphere/src/Misc/input.cpp is there, as I can check using ls. Any idea why?

EDIT:

Changing the first target to relative paths except for the result, i.e.

$(DIR_BLD)/input.o: input.cpp input.h misc_sphere.h
    ${COMP} -c input.cpp ${COMPILE_FLAGS} -o $@

I get a similar error for the output object file:

Fatal error: can't create /home/Jorgen/fqhe_mc_sphere/build/Misc/input.o: No such file or directory

Although the folder /home/Jorgen/fqhe_mc_sphere/build/Misc/ does exist.

I tried running MSYS2 as administrator which didn't help.


Solution

  • You are mixing POSIX paths with Windows paths and running mingw32-make which doesn't understand POSIX paths.

    As user657267 suggests, use make instead. Otherwise you can play tricks with mklink /D to make paths on Windows that are equivalent to the POSIX ones. Here you should be linking C:\home to C:\msys64\home most likely.