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

2020-02-19 04:54发布

I have a simple C program (one source file) which I want to compile on Linux and on Windows via make and nmake, respectively. Is there a possibility to accomplish this with a single makefile?

I thought about something like

ifeq($(MAKE), nmake)
    // nmake code here
else
    // make code here
endif

Unfortunately nmake seems not to understand ifeq, so I cannot use that. I have a working makefile, but that produces very ugly results:

hello: hello.c
    $(CC) hello.c

That works on both systems. The problem is that the outcome depends on the default behaviors of the respective compilers. Under Linux I get an executeable named 'a.out' rather than 'hello'. Under Windows I get 'hello.exe' but there is also 'hello.obj' which I do not want to have.

Is there an alternative way? Or is what I'm trying absolutely impossible?

标签: c makefile nmake
8条回答
放我归山
2楼-- · 2020-02-19 05:42

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

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
查看更多
相关推荐>>
3楼-- · 2020-02-19 05:44

My solution is to use two different filenames. (since the Makefile name searching priority in different OSes will not be the same)

For Windows, I use normal "Makefile."

For Linux, I use the special "GNUmakefile" according to this article.

So that nmake (Win) will find "Makefile," and make (Linux) will find "GNUmakefile."

查看更多
登录 后发表回答