从Linux for Windows交叉编译静态库

时间:2011-01-07 09:26:40

标签: c linux

我想在linux中为windows编译静态库。以下是我编写的程序

  1. 使用i586-mingw32msvc-cc -c static_lib.c -o static_lib.o
  2. 在linux中编译静态库的源代码
  3. 在linux ar rv static_lib.a static_lib.oranlib static_lib.a
  4. 中创建了静态库
  5. 我在windows上的eclipse中创建了一个示例程序,并链接了这个静态库,这个库在linux中用于windows交叉编译。 Windows上使用的编译器是mingw。
  6. 在windows eclipse中编译程序时,编译器给出了以下错误。

    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
    

    守则如下:

    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();
    }
    

    请给我建议编译并使其正常工作。

    此致 约翰尼艾伦

2 个答案:

答案 0 :(得分:2)

尝试使用交叉编译器归档程序而不是原生程序归档程序,即使用i586-mingw32msvc-ari586-mingw32msvc-ranlib而不是arranlib

或者这只是问题的错字?

答案 1 :(得分:1)

尝试i586-mingw32msvc-ar而非普通ar。通常,Linux中的ar不支持用于Windows编程的PE格式。 (或者你必须指示它使用PE格式。)

相关问题