Search code examples
clinuxwindowsgcccross-compiling

How to add gcc libraries and cross compile for windows?


When I use minGW for cross compiling for windows I get these errors. How can I add gcc library and cross compile for windows? I am using x86_64-w64-mingw32-gcc But it compiles with gcc without any issues. The issue arises from having different search.h in gcc and in minGW It hard to show minimum reproducible results here. You can find the source code and the cmake file the following link < https://gitlab.com/DirtyVoid/computer_systems/-/tree/main/Assignment4%20MorseCodeDecoder > If I change the compiler to gcc instead of mingw everything works I am on GNU LINUX but when I try to compile an exe using minGW it does not work The compiler error is due the fact search.h in gcc and mingw has different definitions.

./CMake clean-install
-- Cleaning files and directories
-- Deleting program: MorseCodeDecoder - done
-- Deleting build directory: build/ - done
-- Cleaning - done
-- Creating a new empty build directory:/home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/build/ - done
-- The C compiler identification is GNU 11.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc - works
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/build
[ 20%] Building C object CMakeFiles/MORSE_LIB.dir/src/morse_lib/morse_code.c.o
In file included from /home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/inc/main.h:29,
                 from /home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/src/morse_lib/morse_code.c:1:
/home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/inc/read.h:10:62: warning: 'struct hsearch_data' declared inside parameter list will not be visible outside of this definition or declaration
   10 | extern void read_morse_display_word(FILE *morse_file, struct hsearch_data *);
      |                                                              ^~~~~~~~~~~~
/home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/src/morse_lib/morse_code.c: In function 'load_morse_code_into_hashtable':
/home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/src/morse_lib/morse_code.c:37:9: warning: implicit declaration of function 'hcreate_r' [-Wimplicit-function-declaration]
   37 |         hcreate_r(capacity, hashtable);
      |         ^~~~~~~~~
/home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/src/morse_lib/morse_code.c:43:32: warning: implicit declaration of function 'hsearch_r'; did you mean 'bsearch_s'? [-Wimplicit-function-declaration]
   43 |                 int hsrc_res = hsearch_r(item, ENTER, &item_ptr, hashtable);
      |                                ^~~~~~~~~
      |                                bsearch_s
/home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/src/morse_lib/morse_code.c: In function 'clear_hashtable':
/home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/src/morse_lib/morse_code.c:51:9: warning: implicit declaration of function 'hdestroy_r' [-Wimplicit-function-declaration]
   51 |         hdestroy_r(hashtable);
      |         ^~~~~~~~~~
[ 40%] Building C object CMakeFiles/MORSE_LIB.dir/src/morse_lib/read.c.o
In file included from /home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/inc/main.h:29,
                 from /home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/src/morse_lib/read.c:1:
/home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/inc/read.h:10:62: warning: 'struct hsearch_data' declared inside parameter list will not be visible outside of this definition or declaration
   10 | extern void read_morse_display_word(FILE *morse_file, struct hsearch_data *);
      |                                                              ^~~~~~~~~~~~
/home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/src/morse_lib/read.c:24:6: error: conflicting types for 'read_morse_display_word'; have 'void(FILE *, struct hsearch_data *)' {aka 'void(struct _iobuf *, struct hsearch_data *)'}
   24 | void read_morse_display_word(FILE *morse_file, struct hsearch_data *hashtable) {
      |      ^~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/inc/main.h:29,
                 from /home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/src/morse_lib/read.c:1:
/home/dirtyv/Codes/computer_systems/Assignment4 MorseCodeDecoder/inc/read.h:10:13: note: previous declaration of 'read_morse_display_word' with type 'void(FILE *, struct hsearch_data *)' {aka 'void(struct _iobuf *, struct hsearch_data *)'}
   10 | extern void read_morse_display_word(FILE *morse_file, struct hsearch_data *);
      |             ^~~~~~~~~~~~~~~~~~~~~~~
make[2]: *** [CMakeFiles/MORSE_LIB.dir/build.make:90: CMakeFiles/MORSE_LIB.dir/src/morse_lib/read.c.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/MORSE_LIB.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

Solution

  • hcreate_r and the hsearch_data structure are GNU-specific extensions to the standard POSIX search functions.

    According to the Linux manuap page you must define _GNU_SOURCE before including <search.h> to get that functionality.

    As an extension of the standard it might not be available for MinGW though.