Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using make for cross platform compilation

I am currently developing a C project under Linux and Win32. The 'deliverable' is a shared library, and all the development is done under Linux with the GNU tool chain. I am using a Makefile to compile the shared library.

Every now and then I have to build a .dll under Win32 from the same src.

I've installed MinGW on the Win32 box such that I can use make and get far fewer complaints from the compiler (in comparison to MSVC). I'm at a stage where the src code compiles on both platforms

But the Linux Makefile and Win32 Makefile are different. I'm curious as how to best handle this - should I:

  1. have 2 makefiles, e.g. Makefile for linux and Makefile.WIN32 and then run make -f Makefile.WIN32 on the Windows box

  2. Should I make a different target in a single Makefile and do something like make WIN32 on the Windows box

  3. Should I ditch make and use CMake (is the juice worth the squeeze for such a simple project, i.e. 1 shared library)

like image 745
bph Avatar asked Mar 20 '12 16:03

bph


People also ask

What is cross platform compilation?

A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. For example, a compiler that runs on a PC but generates code that runs on an Android smartphone is a cross compiler.

Why is cross compilation necessary?

A cross compiler is necessary to compile for multiple platforms from one machine. A platform could be infeasible for a compiler to run on, such as for the microcontroller of an embedded system because those systems contain no operating system.

What is a Makefile target?

A simple makefile consists of "rules" with the following shape: target ... : dependencies ... command ... ... A target is usually the name of a file that is generated by a program; examples of targets are executable or object files.


1 Answers

Use a single make file and put the platform-specifics in conditionals, eg

ifeq ($(OS),Windows_NT)     DLLEXT := .dll else     DLLEXT := .so endif  DLL := libfoo$(DLLEXT)  lib : $(DLL) 
like image 89
Christoph Avatar answered Oct 11 '22 18:10

Christoph