Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

makefile

nmake

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?

like image 216
j0ker Avatar asked Nov 25 '11 14:11

j0ker


People also ask

Is nmake the same as make?

A makefile is a text file that contains instructions for how to compile and link (or build) a set of source code files. A program (often called a make program) reads the makefile and invokes a compiler, linker, and possibly other programs to make an executable file. The Microsoft program is called NMAKE.

Can you use makefile on Windows?

If it is a "NMake Makefile", that is to say the syntax and command is compatible with NMake, it will work natively on Windows.

What is the equivalent of make command in Windows?

Look for a Visual C++ makefile; it's usually named Makefile. mak. Then run nmake . But if you only have files named Makefile.in and Makefile.am, then you don't yet have a makable environment.

What is the difference between make and makefile?

The combination of the make command and a makefile provides a very powerful tool for managing projects. It's often used not only to control the compilation of source code, but also to prepare manual pages and to install the application into a target directory. A makefile consists of a set of dependencies and rules.


2 Answers

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.

like image 137
Some programmer dude Avatar answered Oct 11 '22 07:10

Some programmer dude


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 3.10)  # set the project name project(Hello)  # add the executable add_executable(Hello hello.c) 

To build the simple project, you would do the following (this assumes your source and CMakeLists.txt files are in the same directory as the source file hello.c:

mkdir build cd build cmake .. cmake --build . 
like image 42
mevatron Avatar answered Oct 11 '22 07:10

mevatron