Компиляция windows приложений в linux

Compile Windows C console applications in Linux

Best answer so far, and yes, there was a dupe 😀 Also nice to post the needed packages. Straight answer, to the punch.

Thanks for the answer. Also sorry for the duplicate question, I didn’t spot it. 🙂 Keep up the good work! Thanks again.

On Fedora, we also have resources for learning about compiling for win32 using mingw, an irc channel, the mingw32 utilities and a bunch of pre-built libraries for windows — for more info, see: fedoraproject.org/wiki/SIGs/MinGW .

You can if it’s standard C, and doesn’t use Windows libraries.

C code itself is very portable, and the standard C libraries (libc) are available pretty much everywhere. If your code does printf() and sscanf() and fopen() and so on, then it will just compile and run on another platform. Windows, Linux, BSD, etc.

It’s the libraries other than libc that introduce portability challenges.

Anything that links with Windows-specific platform libraries is trouble. Kernel32.lib, user32.lib, etc etc.

There are third-party libs, too, that, if written in C, should be available across Linux and Windows. PCRE is a good example here — it’s a Regular Expression library written in C, and it’s available on Windows as well as on Linux. there are literally hundreds of libraries in this set.

If you confine yourself to libc and library calls into portable libs, then you will have a portable C application.

Источник

Compiling C++ application for Windows target under Linux

Originally published on 2018-10-14 , updated on 2023-05-26 .

Since forever, I have used Arch Linux for day-to-day computing. Nonetheless, from time to time I need to write some simple code in C++ for Windows. Switching back and forth between operating systems is cumbersome, so compiling under a virtual machine is as well.

Читайте также:  Какую версию alt linux выбрать

The Windows port of GCC is named MinGW (Minimalistic GNU for Windows). Quite surprisingly, it is available in the repositories of major Linux distributions, with a wealth of open-source libraries cross-compiled for the Windows platform.

Leveraging this tool, writing multi-platform software can be almost frictionless. It works well in the IDE CLion too. For demonstration purposes, I provide a sample repository which contains a CMake project of a DLL and Windows executables built with MinGW.

Installing MinGW toolchain

Using Arch Linux, I recommend using the ownstuff unofficial repository. The compiler is packaged in a group named mingw-w64 . You might try compiling it from the AUR, but it can be a real pain. There are also many packages available for various dependency libraries; all package names start with mingw-w64- . For more details, please refer to the Arch Wiki.

MinGW is also available in the official repository under Debian — the meta-package gcc-mingw-w64 . There are numerous precompiled libraries, all having mingw in their package names. There is also an entry on the Debian Wiki about this topic.

General configuration

The most important thing is to define the compiler executables and set CMAKE_SYSTEM_NAME :

cmake_minimum_required(VERSION 3.0) project(mingw-test) set(CMAKE_SYSTEM_NAME Windows) SET(CMAKE_C_COMPILER i686-w64-mingw32-gcc) SET(CMAKE_CXX_COMPILER i686-w64-mingw32-g++) SET(CMAKE_RC_COMPILER i686-w64-mingw32-windres) set(CMAKE_RANLIB i686-w64-mingw32-ranlib) set(CMAKE_CXX_FLAGS "$ -std=c++14") set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS) 

Then, you simply define the executable. This requires no further comments.

add_executable(example example.cpp) 

To cross-compile a dynamic loadable library, you use add_library . However, for convenience, it is better to set PREFIX and SUFFIX to empty strings. This way, you can use the whole filename (with the .dll extension) as the target name.

add_library(shared_lib.dll SHARED shared_lib.cpp shared_lib.h) set_target_properties(shared_lib.dll PROPERTIES PREFIX "" SUFFIX "" LINK_FLAGS "-Wl,--add-stdcall-alias" POSITION_INDEPENDENT_CODE 0 # this is to avoid MinGW warning; # MinGW generates position-independent-code for DLL by default ) 

To link the executable with the aforementioned DLL, you add:

target_link_libraries(example shared_lib.dll) 

Using pkg-config

The common way of defining library compilation flags and directories under Unix is using the pkg-config tool. This way, your codebase is not dependent on the exact layout of directories of the distribution. Each library provides a .pc file with build parameters such as header locations, compiler flags, etc. Many packages compiled for MinGW also provide .pc files, at least under Debian and Arch Linux.

Читайте также:  Linux files access rights

For example, let’s add zlib as a dependency:

Load PkgConfig package and find zlib:

include(FindPkgConfig) find_package(PkgConfig REQUIRED) pkg_check_modules(ZLIB "zlib") 

Add dependencies to example executable:

include_directories($ZLIB_INCLUDE_DIRS>) target_link_libraries(example $ZLIB_LIBRARIES>) 

Adding runtime libraries to Wine

When you try to run your freshly cross-compiled program under Wine, it will probably fail with a message like this:

0009:err:module:import_dll Library libstdc++-6.dll (which is needed by L"your-program.exe") not found 

It means that Wine cannot find the runtime libraries on which your program depends. They are usually located under /usr/i686-w64-mingw32/bin . You’ll need to add this path to the PATH variable within the Wine subsystem.

To do so, edit the file ~/.wine/system.reg . This file represents the Windows Registry under Wine. Find the PATH variable definition under [System\\CurrentControlSet\\Control\\Session Manager\\Environment] section. Append the library path translated into a Windows-like path: Z:\usr\i686-w64-mingw32\bin so it looks like this:

"PATH"=str(2):"C:\\windows\\system32;C:\\windows;C:\\windows\\system32\\wbem;Z:\\usr\\i686-w64-mingw32\\bin" 

Running with Wine under Clion

Your CMake project should be loaded by Clion without issues. Windows-specific headers like windows.h are also available. The build will configure itself automatically, but running the target executables will not. Fortunately, you can modify the generated Run/Debug configurations to call Wine, as shown in the picture:

Run/Debug configuration window. My Github page

Atom/RSS feed. Copyright by Michał Słomkowski. This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. This website is a perpetual work in progress. Last compilation: 2023-07-07 .

Michał Słomkowski — software developer, tinkerer and hardware enthusiast . Based in Poznań, Poland . Feel free to contact me if you have any questions or ideas. If you’re a human being, you can easily decode my e-mail: michal [‘at’ character] domain this site is hosted on. Please inform me about any errors and inaccuracies you stumble upon.

Источник

Компиляция приложений Windows в Linux

Одна из потрясающих особенностей Linux состоит в том, что, работая в нем, вы можете создавать приложения Windows. Такую возможность обеспечивает пакет mingw-w64. В его состав входят компиляторы для создания 32- и 64-битного кода С и С++ для систем Windows. После установки пакета mingw-w64 генерация кода Windows осуществляется следующим образом:

# C i686-w64-mingw32-gcc hello.c -o hello32.exe # 32-bit x86_64-w64-mingw32-gcc hello.c -o hello64.exe # 64-bit # C++ i686-w64-mingw32-g++ hello.cc -o hello32.exe # 32-bit x86_64-w64-mingw32-g++ hello.cc -o hello64.exe # 64-bit

Компиляторы имеют сложные названия, поэтому лучше всего оформить их в виде make-файла или скрипта.

Компилируемые программы на C будут зависеть от библиотек kernel32.dll и msvcrt.dll. Первая является неотъемлемой частью ОС Windows. Вторая представляет собой стандартную библиотеку времени выполнения C от компании Microsoft и обычно также входит в состав ее операционной системы.

Программы на C++ будут также зависеть от библиотек libstdC++-6.dll и libgcc_s_sjlj-1.dll. Это библиотеки компиляторов Mingw. Они находятся в каталоге /usr/lib/gcc/x86_64-w64-mingw32/. Для того, чтобы статически включить их в состав вашего исполняемого модуля укажите при компиляции ключи:

-static-libgcc -static-libstdc++

P.S. при написании этого поста использовались материалы блога arrayfire (точнее статьи).

P.P.S. кто-нибудь может назвать мне хотя бы один компилятор в Windows, позволяющий создавать двоичный код для Linux?

Источник

Оцените статью
Adblock
detector