Cross compile Static Library from Linux for windows

When developing software, there is a high chance that software might be used in various operating systems. Therefore, it is important to build software in a cross-platform manner to ensure compatibility with multiple operating systems. In this article, we will focus on cross-compiling static libraries from Linux for Windows.

What is a Static Library?

A static library is a file that contains pre-compiled code that can be linked with an executable program to create a single, self-contained executable. The code in a static library is linked directly into the executable, which makes it faster and more efficient than dynamic libraries.

What is Cross-Compiling?

Cross-compiling is the process of building software on one platform that is intended to run on a different platform. For example, building a Windows application on a Linux machine.

Why Cross-Compile?

Cross-compiling has several benefits, such as

  • Increased productivity Cross-compiling saves time, allowing developers to build for multiple platforms without having to switch between different machines.

  • Improved portability Cross-compiling ensures that software is compatible with multiple platforms.

  • Reduced development costs Cross-compiling eliminates the need for multiple machines and operating systems, reducing hardware and software costs.

  • Easier testing Cross-compiling enables developers to test their software on multiple platforms, without the need for physical hardware.

Cross-Compiling Process Overview

Cross-Compilation Workflow Source Code (Linux) Cross-Compiler (Mingw-w64) Object Files (.o files) Static Library (.a file for Windows) Test Code (main.c) Link with Static Library Windows Executable (.exe file) Uses

Step-by-Step Implementation

Step 1 Install Required Cross-Compiler

The first step is to install a cross-compiler that will allow us to compile our code for Windows on a Linux machine. For this tutorial, we will use Mingw-w64 cross-compiler.

To install Mingw-w64 cross-compiler on Ubuntu, run the following command

sudo apt-get install mingw-w64

Step 2 Create Source Code

Create a simple static library with a "hello world" function. Create a file called hello.c

#include <stdio.h>

void hello() {
   printf("Hello, world!<br>");
}

Step 3 Compile to Object File

Compile the source code into an object file using the cross-compiler

x86_64-w64-mingw32-gcc -c hello.c -o hello.o

This command compiles hello.c into an object file called hello.o compatible with Windows.

Step 4 Create Static Library

Create the static library from the object file using the archiver tool

x86_64-w64-mingw32-ar rcs libhello.a hello.o

This creates a static library file libhello.a from the object file hello.o.

Step 5 Create Test Application

Create a Windows application to test the static library. Create main.c

#include <windows.h>
extern void hello();

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
   hello();
   return 0;
}

Step 6 Compile Windows Application

Compile the test application and link it with the static library

x86_64-w64-mingw32-gcc main.c -o main.exe -L. -lhello -mwindows

The -L. option tells the compiler to look for libraries in the current directory, and -lhello links with the libhello.a static library.

Key Considerations

Aspect Consideration Solution
Platform-specific Code Code may behave differently on Windows Use portable C/C++ constructs
Testing Cannot run Windows executables on Linux Test on actual Windows systems or Wine
Dependencies External libraries may not be available Use static linking or provide Windows versions

Common Cross-Compiler Tools

Tool Purpose Usage
x86_64-w64-mingw32-gcc C/C++ compiler Compile source to object files
x86_64-w64-mingw32-ar Archiver Create static libraries (.a files)
x86_64-w64-mingw32-ld Linker Link object files and libraries

Conclusion

Cross-compiling static libraries from Linux for Windows using Mingw-w64 is an efficient way to develop cross-platform software. This approach saves development time, reduces costs, and ensures compatibility across multiple operating systems without requiring separate development environments for each target platform.

Updated on: 2026-03-17T09:01:38+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements