0

Long story short, this library https://github.com/tum-vision/fastms has a makefile for c++ compilation to Matlab files for mac and linux, Im trying to repurpose it for cygwin.

Please please please, I am very desperate to get this to work after spending a week on this single makefile.

Here is my make file in its current iteration

all: targets

USE_CUDA:=0
USE_OPENMP:=1
USE_OPENCV:=0
USE_MEX:=1

TMP_DIR:=tmp



LIBS:=
DEFINES:=
INCLUDES:=
TARGETS:=

SOLVER_SOURCE_DIR:=src/libfastms
INCLUDES += -I$(SOLVER_SOURCE_DIR)



# c++

GXX:=x86_64-w64-mingw32-gcc
#GXX:=g++
ARGS_GXX:=
ARGS_GXX += -Wall
ARGS_GXX += -O3
ARGS_GXX += -m64
ARGS_GXX += -fPIC
ifeq ($(USE_OPENMP), 1)
    ARGS_GXX += -fopenmp
endif
COMMAND_COMPILE_GXX=$(GXX) -c -o $@ $< $(ARGS_GXX) $(INCLUDES) $(DEFINES)
COMMAND_GET_DEPENDENCIES_GXX=@$(GXX) -M $< $(ARGS_GXX) $(INCLUDES) $(DEFINES) > $@.dep


# openmp  
ifeq ($(USE_OPENMP), 1)
    LIBS += -lgomp
else
    DEFINES += -DDISABLE_OPENMP
endif

DEFINES += -DDISABLE_OPENCV
DEFINES += -DDISABLE_CUDA

# target: solver
SOLVER_SOURCES:=
SOLVER_SOURCES += $(shell find $(SOLVER_SOURCE_DIR) -name '*.cpp')
SOLVER_OBJECTS:=$(foreach file, $(SOLVER_SOURCES), $(TMP_DIR)/$(file).o)
SOLVER_DEPENDENCIES:=$(foreach file, $(SOLVER_OBJECTS), $(file).dep)
-include $(SOLVER_DEPENDENCIES)
SOLVER_TARGET:=$(TMP_DIR)/$(SOLVER_SOURCE_DIR)/libfastms.o
TARGETS += $(SOLVER_TARGET)
COMMAND_LINK_SOLVER=ld -r -o $@ $^


# target: main
MAIN_SOURCES:=
MAIN_SOURCES += $(shell find src/examples -name '*.cpp')
MAIN_OBJECTS:=$(foreach file, $(MAIN_SOURCES), $(TMP_DIR)/$(file).o)
MAIN_OBJECTS += $(SOLVER_TARGET)
MAIN_DEPENDENCIES:=$(foreach file, $(MAIN_OBJECTS), $(file).dep)
-include $(MAIN_DEPENDENCIES)
MAIN_TARGET:=main
TARGETS += $(MAIN_TARGET)
COMMAND_LINK_MAIN=$(GXX) -o $@ $^ $(LIBS)



# mex
ifeq ($(USE_MEX), 1)

    MATLAB_DIR:=/c/Program\ Files/MATLAB/r2017a

    ifeq ($(wildcard $(MATLAB_DIR)/bin/win64),)
        $(info WARNING: Did not find MATLAB in the specified directory $(MATLAB_DIR), disabling mex target compilation.)
        USE_MEX:=
    endif

endif
ifeq ($(USE_MEX), 1)

    MEX_SUFFIX:=mexw64

    MEX_SOURCES:=$(shell find src/mex -name '*.cpp')
    MEX_OBJECTS:=$(foreach file, $(MEX_SOURCES), $(TMP_DIR)/$(file).o)
    MEX_OBJECTS += $(SOLVER_TARGET)
    MEX_DEPENDENCIES:=$(foreach file, $(MEX_OBJECTS), $(file).dep)
    -include $(MEX_DEPENDENCIES)

    MEX_DEFINES := $(DEFINES)

    MEX_INCLUDES :=$(INCLUDES) -I$(MATLAB_DIR)/extern/include
    MATLAB_LIB_DIR1:=$(MATLAB_DIR)/extern/lib/win64/mingw64
    MATLAB_LIB_DIR2:=$(MATLAB_DIR)/bin/win64

    MEX_LIBS:=$(LIBS) -L$(MATLAB_LIB_DIR1) -L$(MATLAB_LIB_DIR2) -lmex -lmx

    MEX_TARGET:=examples/matlab/fastms_mex.$(MEX_SUFFIX)
    TARGETS += $(MEX_TARGET)

    COMMAND_COMPILE_GXX_MEX=$(GXX) -c -o $@ $< $(ARGS_GXX) $(MEX_INCLUDES) $(MEX_DEFINES)
    COMMAND_GET_DEPENDENCIES_GXX_MEX=@$(GXX) -M $< $(ARGS_GXX) $(MEX_INCLUDES) $(MEX_DEFINES) > $@.dep
    COMMAND_LINK_MEX=$(GXX) -o $@ $^ -shared $(MEX_LIBS)
else
    DEFINES += -DDISABLE_MEX
endif 



# common commands
COMMAND_POSTPROCESS_DEPENDENCIES=@echo $@:`sed 's/.*://' $@.dep | tr "\n" " " | sed 's/\\\\/ /g'` > $@.dep; sed -e 's/^.*://' -e 's/  */::/g' $@.dep | tr ":" "\n" | sed -e 's/$$/:/' -e 's/^:$$//' >> $@.dep; echo >> $@.dep
COMMAND_MAKE_TARGET_DIR=@mkdir -p $(shell dirname $@)
COMMAND_CLEAN=@rm -rf $(TMP_DIR) $(TARGETS)



targets: $(TARGETS)

# solver

$(TMP_DIR)/$(SOLVER_SOURCE_DIR)/%.cpp.o: $(SOLVER_SOURCE_DIR)/%.cpp Makefile

    $(COMMAND_MAKE_TARGET_DIR)
    $(COMMAND_COMPILE_GXX)
    $(COMMAND_GET_DEPENDENCIES_GXX)
    $(COMMAND_POSTPROCESS_DEPENDENCIES)

$(TMP_DIR)/$(SOLVER_SOURCE_DIR)/%.cu.o: $(SOLVER_SOURCE_DIR)/%.cu Makefile
    $(COMMAND_MAKE_TARGET_DIR)
    $(COMMAND_NVCC_COMPILE)
    $(COMMAND_GET_DEPENDENCIES_NVCC)
    $(COMMAND_POSTPROCESS_DEPENDENCIES) 

$(SOLVER_TARGET): $(SOLVER_OBJECTS)
    $(COMMAND_MAKE_TARGET_DIR)
    $(COMMAND_LINK_SOLVER)



# main

$(TMP_DIR)/src/examples/%.cpp.o: src/examples/%.cpp Makefile
    $(COMMAND_MAKE_TARGET_DIR)
    $(COMMAND_COMPILE_GXX)
    $(COMMAND_GET_DEPENDENCIES_GXX)
    $(COMMAND_POSTPROCESS_DEPENDENCIES)

$(MAIN_TARGET): $(MAIN_OBJECTS)
    $(COMMAND_MAKE_TARGET_DIR)
    $(COMMAND_LINK_MAIN)



# mex
$(TMP_DIR)/src/mex/%.cpp.o: src/mex/%.cpp Makefile
    $(COMMAND_MAKE_TARGET_DIR)
    $(COMMAND_COMPILE_GXX_MEX)
    $(COMMAND_GET_DEPENDENCIES_GXX_MEX)
    $(COMMAND_POSTPROCESS_DEPENDENCIES)

$(MEX_TARGET): $(MEX_OBJECTS)
    $(COMMAND_MAKE_TARGET_DIR)
    $(COMMAND_LINK_MEX)



clean:
    $(COMMAND_CLEAN)

When I use g++ as my compiler, the problem is (a) it does not support the -m64 bit flag and (b) it can't find the libraries -lmex -lmx despite the fact that I am linking the directory of libmex.lib and libmx.lib in the make file (see MEX_LIBS) . Some guides online suggest to link the directories of the dll files directly limbex.dll and libmx.dll but I get an error like "unsupported file format"

When I use x86_64-w64-mingw32-gcc as my compiler as some posts suggest, I run into the error: tmp/src/libfastms/solver/solver.cpp.o: file not recognized: File format not recognized. Posts online say this is because of residual .o files from previous builds but I have made sure these are all clean. I have no idea why this is.

Alex Kyriazis
  • 311
  • 1
  • 4
  • 11
  • 1
    `-m64` is not needed as x86_64-w64-mingw32-gcc is already 64bit. Have you installed mingw64-x86_64-binutils ? Can you compile without the makefile,issuing the commands directly ? – matzeri Jun 08 '17 at 11:06

1 Answers1

2

You can do away with the Makefile and simply compile everything directly from MATLAB, to produce a self-contained MEX-file.

I have VS2015 configured as compiler, but you can use any compiler supported by your MATLAB version (run mex -setup and select one, including MinGW-w64 if you want).

  1. download and extract source code, say C:\fastms

  2. The code implements a simple stopwatch in src\libfastms\util\timer.h to report time stats. It uses gettimeofday from <sys/time.h> which is not available if you're using MSVC (it's a POSIX header). You could find a Windows replacement for it, but since it's not really vital to the implementation, I just replaced it with a dummy function:

    //#include <sys/time.h>   // <-- comment this line
    ...
    double get_cur_seconds()
    {
      return 0;  // <-- replace code with just this
    }
    
  3. start MATLAB and run the following:

    cd('C:\fastms')
    mex -DDISABLE_CUDA -DDISABLE_OPENCV -largeArrayDims -outdir examples\matlab src\mex\fastms_mex.cpp src\libfastms\solver\*.cpp src\libfastms\util\*.cpp -Isrc\mex -Isrc\libfastms -Isrc\libfastms\solver -Isrc\libfastms\util
    
  4. now you can test the library with one of the example included:

    cd('C:\fastms\examples\matlab')
    example1()
    

result

Amro
  • 123,847
  • 25
  • 243
  • 454