Use the same makefile for make (Linux) and nmake (Windows)

天大地大妈咪最大 提交于 2019-11-30 03:12:23

It's probably not impossible, but most likely so hard that it would be easier to write two makefiles anyway.

Both GNU make (used in Linux) and nmake have include directives though, so some common things can be put in a common makefile that is included by the main makefile.

You should look at using CMake for this. With one source file it should be quite easy!

EDIT :

Here is how you could set up a simple project:

cmake_minimum_required(VERSION 2.8)

project(Simple)

include_directories("${PROJECT_BINARY_DIR}")

add_executable(Simple simple.cpp)

To build the simple project, you would do the following (this assumes your source and CMakeLists.txt files are in ~/src/simple:

foo@bar~/src/simple$ mkdir build
foo@bar~/src/simple$ cd build
foo@bar~/src/simple$ cmake ..
foo@bar~/src/simple$ make

I wanted to use the same makefile include to be used by make and nmake. Since make recognises line continuation on comment lines but nmake doesn't, this means that we can have separate instructions for make and nmake. eg:

# \
!ifndef 0 # \
# nmake code here \
MV=move # \
RM=del # \
CP=copy # \
!else
# make code here
MV=mv -f
RM=rm -f
CP=cp -f
# \
!endif

Just have to make sure that nmake specific code is terminated by # \

I am not able to find a way to use a common makefile to work for both GNU make and MS nmake, mainly because they have an incompatible syntax for "include" and/or "if" directives. MS nmake requires to use ! prefix for directives. eg, !if, !include, etc...

If it is allowed to have separate macros, however, it could be tricked around. Here I presents the best way I found so far for making makefile compatible for both GNU make and MS nmake by observing the followings:

  1. MS nmake reads TOOLS.ini file for default macros.
  2. MS suite uses .obj as object file extension.
  3. GNU make reads files defined in a MAKEFILES environment variable.
  4. GNU suite use .o as object file extension.
  5. GNU make need not give an executable extension .exe for a target.

Note: The following has been tested using MS Visual Studio 2015 and MINGW32.

Step 1: create a following DOS batch file and let it run whenever the CMD prompt is invoked.

set MAKEFILES=TOOLS.gcc
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"

Step 2: Create a TOOLS.ini file under your working directory as below: (this file is independent of your project dependencies except the libraries possibly)

[NMAKE] 
LDLIBS  =
CDEBUG  = /Zi
LDEBUG  = /debug:full
WDFLAGS = /wd4996 /wd4774 /wd4018 /wd4710 /wd4820
CFLAGS  = /nologo $(CDEBUG) /EHsc /Wall $(WDFLAGS)
LDFLAGS = /nologo $(LDEBUG)
RM      = del /F /Q
LINK    = "$(VCINSTALLDIR)bin\link" $(LDFLAGS)
CP  = copy
CC  = cl
CPP = $(CC) /P
X   = .exe
O   = .obj

.obj.exe:
    $(LINK) $** $(LOADLIBES) $(LDLIBS) /Out:$@

Step 3: Create a TOOLS.gcc under your working directory as below: (this file is independent of your project dependencies except the libraries possibly)

LD_LIBS = 
LDLIBS  =
CDEBUG  = -g
LDEBUG  = -g
CFLAGS  = $(CDEBUG)
LDFLAGS = $(LDEBUG)
RM      = rm -f
LINK    = gcc $(LDFLAGS)
CP      = cp
CC      = gcc
CPP     = $(CC) -E
X       =
O       = .o

%: %.o
    $(LINK) $^ $(LOADLIBES) $(LDLIBS) -o $@

Step 4: Edit your makefile as below (note $(X) and $(O)) where only dependecies are specified.

SHELL    = /usr/bin/sh
app: app1$(X) app2$(X)
app1$(X): app1$(O)
app2$(X): app2$(O)

clean:
    $(RM) *.exe *.o *.obj *.ilk *.pdb *.tmp *.i *~

Step 5: Enjoy GNU make and MS nmake with the same makefile

$ nmake
$ make clean
$ nmake clean
$ make

I just thought of something completely different.

If you stick to your extremely simple Makefile, which, you say, works, and just put the 'standard' variables CC and CFLAGS in your respective environments, say

  export CC=gcc

respectively

  set CC=CL.EXE

and

  export CFLAGS=-o myexecutable

respectively

  set CFLAGS=/out:myexecutable.exe

it might just work.

Be aware, I'm not firm in the exact options to use, you'll have to figure them out yourself. But AFAIK both make variants recognize the same set of flags. You may even set those on the respective command lines (but not in the makefile, since NMAKE uses a different 'ifeq' syntax...)

Yes, you can do this with a single Makefile. The best source for this material is the O'Reilly Book:

Managing Projects with GNU Make, Third Edition By Robert Mecklenburg

See chapter 7: Portable Makefiles.

In Summary, the technique is to test the environment variable ComSpec which says if the windows command interpreter is present:

ifdef COMSPEC
 MV ?= move
 RM ?= del
else
 MV ?= mv -f
 RM ?= rm -f
endif

I wrap this with a portable shell script which uses sed to edit the makefile for Nmake or Gnu-make..

I've recently experimented with using the C preprocessor to generate a portable Makefile from a template Makefile.cc containing preprocessor symbols. So far its worked surprisingly well. The first observation is that NMake will prescan a Tools.ini file, which I provide in the same directory as

[NMAKE]
MAKECONFIG=-D_NMAKE

Then I have a 'true' Makefile next to it which is written in only the common sub language of GNU Make and NMake.

MAKEFILE=Makefile.mk
TEMPLATE=Makefile.cc

all: $(MAKEFILE)
    $(MAKE) -f $(MAKEFILE)

clean: $(MAKEFILE)
    $(MAKE) -f $(MAKEFILE) clean

$(MAKEFILE): $(TEMPLATE)
    $(CXX) $(MAKECONFIG) -E $(TEMPLATE) > $(MAKEFILE)

Note that the -E switch is pretty common for compilers (at least the big 3 I work with: GCC, Clang, CL) for only preprocessing the file. With GNU Make the $(MAKECONFIG) expands to nothing but in NMake it provides the preprocessor variable declaring itself. Since your template Makefile.cc can check it with #ifdef, as well as check for common variables with which the compiler declares itself, you can customize your Makefile.mk quite a bit for both the 'make' program, your operating system, and the compiler you're using. If you have any 'make' you probably already have a C compiler too; there's no need to install additional software like CMake or autotools. It uses mechanisms that are old and so likely to work in a lot of environments. And from what I've been able to tell so far, it's really fast. Faster at least than running a configuration step in autotools. The only disadvantage I've faced is that it limits the style of your make rules to being on the same line, because the preprocessor changes the indentation of the code. Also the preprocessor spits out lines with # tags but since these start a comment in a Makefile they get ignored anyway.

A have a somewhat small C++ project with a Makefile.cc that looks like the following snippet. It compiles on GNU Make or NMake with either GCC, Clang or CL and on either Windows or in a POSIX environment. I've yet to support BSD Make or test any other compiler though.

// Make Version

#ifdef _NMAKE
# define ifdef !ifdef
# define ifndef !ifndef
# define else !else
# define endif !endif
# define err(x) !error x
# define cat(x, y) x=$(x) y
#else // GNU Make
# define err(x) $(error x)
# define cat(x, y) x += y
#endif

// System Commands

ifdef SHELL
RM=rm -f
else
ifdef COMSPEC
RM=del /f
else
err("Cannot determine your system commands.")
endif // COMSPEC
endif // SHELL

// Project Variables

STD=c++17
SRC=test.cpp dbg.cpp dir.cpp dll.cpp env.cpp err.cpp fifo.cpp file.cpp shm.cpp sig.cpp socket.cpp sys.cpp xdg.cpp
BIN=test

.SUFFIXES: .cpp .hpp .o .d .obj .pdb .lib .exp .ilk .log .i .db

// Operating Sytem

#ifdef _WIN32
cat(CFLAGS, -D_WIN32)
EXE=$(BIN).exe
#else
cat(CFLAGS, -D_POSIX_C_SOURCE)
cat(LDFLAGS, -ldl -lrt -lpthread)
EXE=$(BIN)
#endif

// Make Targets

all: $(EXE)

clean: ; $(RM) $(EXE) *.o *.d *.obj *.pdb *.lib *.exp *.ilk *.log *.i

// Compiler Options

#ifdef _MSC_VER

cat(CFLAGS, -nologo -std:$(STD) -W4 -DNOMINMAX -D_CRT_SECURE_NO_WARNINGS -EHsc -permissive-)
ifndef NDEBUG
cat(CFLAGS, -Zi)
endif
cat(LDFLAGS, -nologo)

OBJ=$(SRC:.cpp=.obj)

$(EXE): $(OBJ); $(CXX) $(LDFLAGS) $(OBJ) -Fe$@
.cpp.obj: ; $(CXX) $(CFLAGS) -c $<

#elif defined(__GNUC__) || defined(__llvm__) || defined(__clang__)

cat(CFLAGS, -std=$(STD) -Wall -Wextra -Wpedantic -MP -MMD)
ifndef NDEBUG
cat(CFALGS, -g)
endif
cat(LDFLAGS, -rdynamic)

OBJ=$(SRC:.cpp=.o)

$(EXE): $(OBJ); $(CXX) $(LDFLAGS) $(OBJ) -o $@
.cpp.o: ; $(CXX) $(CFLAGS) -c $<

# ifndef _NMAKE
-include $(SRC:.cpp=.d)
# endif
#else
# error "Cannot determine your compiler."
#endif
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!