Michas
Michas

Reputation: 9428

Import variables from C preprocessor into Makefile

I want to create one Makefile for Windows and Linux builds. The problem is I have to link with different dynamic libraries for each platform. The C preprocessor may have few nice variables, for example _WIN32. How to extract this information?

The solution have to work with a cross compiler. I cannot create and then run a small program. I have only one, different variable, the CC, the environment may be the same.

The other way around is easy, the -D switch.


Similar but different questions:

Makefile that distincts between Windows and Unix-like systems
I use the same make program. Only the CC variable is different.

Upvotes: 0

Views: 269

Answers (2)

Michas
Michas

Reputation: 9428

Based on answer from TTK

Create file with variables, something like this:

#ifdef _WIN32
-lgdi32 -lopengl32
#else
-lpthread -lX11 -lXxf86vm -lXcursor -lXrandr -lXinerama -lXi -lGL
#endif

And something like this to the Makefile:

A_FLAGS = $(strip $(shell $(CPP) -P $(CPPFLAGS) ./a_flags.cpp))
# or
A_FLAGS = $(strip $(shell $(CPP) -P -xc $(CPPFLAGS) ./a_flags))

$(CPP) – the C preprocessor executable, should be defined by the Make program
-P – the flag to inhibit generation of linemarkers
-xc – the flag to force preprocessor to treat file as C source
$(CPPFLAGS) – optional flags for the preprocessor defined by programmer

Upvotes: 0

TTK
TTK

Reputation: 223

I don't know if you can get directly those variables but you can try this solution:

CPP=i686-w64-mingw32-cpp
CPPFLAGS= -P
WIN32=$(shell echo _WIN32 | $(CPP) $(CPPFLAGS))

iswin32:
    @echo $(WIN32)

This example will output 1:

$ make iswin32
1

If you are dealing with multiple declarations consider also creating a file with all the declarations, preprocess it and include it in a makefile.

$ cat declaration
WIN32 = _WIN32

Upvotes: 1

Related Questions