Cross compile with from Linux to Windows "fatal error: windows.h: No such file or directory"

I am trying to cross compile on linux using x86_64-w64-mingw32-gcc. But, I get this error:

/usr/local/x86_64-w64-mingw32/include/SDL2/SDL_syswm.h:55:10: fatal error: windows.h: No such file or directory

I installed the corresponding package, ran make cross and ended up with this makefile after a lot of trial and error. Where have I gone wrong here? I know that windows.h exists in the include directory in /usr/local.

ifeq ($(os),windows)
TARGET ?= bin/hellogame.exe

CCX := x86_64-w64-mingw32-gcc
CXXFLAGS := --std=c++17 -g -Wall -I include -I imgui -w -fpermissive -I/usr/local/x86_64-w64-mingw32/include -L/usr/local/x86_64-w64-mingw32/lib -Llib/SDL2-2.0.9/x86_64-w64-mingw32/lib -lSDL2main -lSDL2 -lws2_32
LDFLAGS := -static -static-libgcc
# LIBS := $(shell /usr/local/x86_64-w64-mingw32/bin/sdl2-config --static-libs)
else
TARGET ?=bin/hellogame

CXX := g++
CXXFLAGS := --std=c++17 -m64 -g -Wall -I include -I imgui -MMD
LDFLAGS :=
LIBS := -lSDL2 -lSDL2_image
endif






IMGUI_DIR = imgui
IMGUI_SRCS = $(wildcard $(IMGUI_DIR)/*.cpp)
IMGUI_OBJS = $(patsubst $(IMGUI_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(IMGUI_SRCS))

SRC_DIR = src
BUILD_DIR = build

# Get all source files in the src directory
SRCS = $(wildcard $(SRC_DIR)/*.cpp)
# Generate corresponding object file names in the build directory
OBJS = $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SRCS))






# Default target
all: $(TARGET)

# Rule to build the executable
$(TARGET): $(OBJS) $(IMGUI_OBJS)
	$(CXX) $(LDFLAGS) $(OBJS) $(IMGUI_OBJS) -o $@ $(LIBS)

# Rule to build object files from source files
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
	$(CXX) $(CXXFLAGS) -c $< -o $@

# Rule to build ImGui object files
$(BUILD_DIR)/%.o: $(IMGUI_DIR)/%.cpp
	$(CXX) $(CXXFLAGS) -c $< -o $@
	

# Include automatically generated dependencies
-include $(BUILD_DIR)/*.d

# Target to build and run the executable
build: $(TARGET)
	./$(TARGET)

# Clean target to remove generated files
clean:
	rm -f $(BUILD_DIR)/*.o $(BUILD_DIR)/*.d $(TARGET) $(BUILD_DIR)/imgui.ini

.PHONY: all clean

In the conditional cross compilation block you’ve got CCX instead of the correct CXX for selecting your C++ compiler. That’s the first thing that stands out here.

pe 3. marrask. 2023 klo 8.00 Connor via discourse.libsdl.org <noreply@discourse.libsdl.org> kirjoitti:

Can we see your makefile ?

Thank you! I changed a couple things around after that, but the misnamed variable was the lead cause of my problem. In case it’ll help anyone in the future, this is the makefile I ended up with:

ifeq ($(os),windows)
TARGET ?= bin/hellogame.exe

CXX := x86_64-w64-mingw32-g++
CXXFLAGS := --std=c++17 -g -Wall -w -fpermissive -I include -I imgui -I/usr/local/x86_64-w64-mingw32/include
LDFLAGS := -static -static-libgcc
LIBS := $(shell /usr/local/x86_64-w64-mingw32/bin/sdl2-config --static-libs) -lSDL2_image
else
TARGET ?=bin/hellogame.out

CXX := g++
CXXFLAGS := --std=c++17 -m64 -g -Wall -I include -I imgui -MMD
LDFLAGS :=
LIBS := -lSDL2 -lSDL2_image
endif






IMGUI_DIR = imgui
IMGUI_SRCS = $(wildcard $(IMGUI_DIR)/*.cpp)
IMGUI_OBJS = $(patsubst $(IMGUI_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(IMGUI_SRCS))

SRC_DIR = src
BUILD_DIR = build

# Get all source files in the src directory
SRCS = $(wildcard $(SRC_DIR)/*.cpp)
# Generate corresponding object file names in the build directory
OBJS = $(patsubst $(SRC_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(SRCS))






# Default target
all: $(TARGET)

# Rule to build the executable
$(TARGET): $(OBJS) $(IMGUI_OBJS)
	$(CXX) $(LDFLAGS) $(OBJS) $(IMGUI_OBJS) -o $@ $(LIBS)

# Rule to build object files from source files
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
	$(CXX) $(CXXFLAGS) -c $< -o $@

# Rule to build ImGui object files
$(BUILD_DIR)/%.o: $(IMGUI_DIR)/%.cpp
	$(CXX) $(CXXFLAGS) -c $< -o $@
	

# Include automatically generated dependencies
-include $(BUILD_DIR)/*.d

# Target to build and run the executable
.PHONY: build
build: $(TARGET)
	./$(TARGET)

# Clean target to remove generated files
clean:
	rm -f $(BUILD_DIR)/*.o $(BUILD_DIR)/*.d $(TARGET) $(BUILD_DIR)/imgui.ini

.PHONY: all clean