Johnnie
Johnnie

Reputation: 235

Cross compile Static Library from Linux for windows

I want to compile static library in linux for windows. Following is the procedure I followed for compiling

  1. Compile the source code of static library in linux using i586-mingw32msvc-cc -c static_lib.c -o static_lib.o
  2. Created the static library in linux ar rv static_lib.a static_lib.o and ranlib static_lib.a
  3. I created a sample program in eclipse on windows and linked this static library which is cross compiled in linux for windows. The compiler used at windows was mingw.

while compiling the program in windows eclipse, the compiler gives me the following error.

static_test\static_lib.a: file format not recognized; treating as linker script 
\static_test\static_lib.a:1: syntax error
collect2: ld returned 1 exit status
Build error occurred, build is stopped

The Codes are as follows:

static_lib.c

#include <stdio.h>

void func(void)
{
        printf("Hello\n");
        printf("Hello\n");
        printf("Hello\n");
        printf("Hello\n");
        printf("Hello\n");
        printf("Hello\n");
        printf("Hello\n");
        printf("Hello\n");
        printf("Hello\n");
        printf("Hello\n");
        printf("Hello\n");
        printf("Hello\n");
}

sample_static.c

#include <stdio.h>

extern void func(void);

int main ()
{
    printf ("Main function\n");
    func();
}

kindly give me suggestions to compile and get it work.

Regards Johnnie Alan

Upvotes: 5

Views: 3088

Answers (2)

Prof. Falken
Prof. Falken

Reputation: 24937

Try i586-mingw32msvc-ar instead of plain ar. Typically ar in Linux will not support the PE format used for Windows programming. (Or you will have to instruct it to use PE format.)

Upvotes: 1

Christoffer
Christoffer

Reputation: 12910

Try using the cross-compiler archiver instead of the native one, i.e. use i586-mingw32msvc-ar and i586-mingw32msvc-ranlib instead of ar and ranlib.

Or is that just a typo on the question?

Upvotes: 2

Related Questions