Creating a DLL file of C program on ubuntu [duplicate] - c

I have written some effects in C++ (g++) using freeglut on Linux, and I compile them with
g++ -Wall -lglut part8.cpp -o part8
So I was wondering if it is possible to have g++ make static compiled Windows executables that contains everything needed?
I don't have Windows, so it would be really cool, if I could do that on Linux :)

mingw32 exists as a package for Linux. You can cross-compile and -link Windows applications with it. There's a tutorial here at the Code::Blocks forum. Mind that the command changes to x86_64-w64-mingw32-gcc-win32, for example.
Ubuntu, for example, has MinGW in its repositories:
$ apt-cache search mingw
[...]
g++-mingw-w64 - GNU C++ compiler for MinGW-w64
gcc-mingw-w64 - GNU C compiler for MinGW-w64
mingw-w64 - Development environment targeting 32- and 64-bit Windows
[...]

Suggested method gave me error on Ubuntu 16.04: E: Unable to locate package mingw32
===========================================================================
To install this package on Ubuntu please use following:
sudo apt-get install mingw-w64
After install you can use it:
x86_64-w64-mingw32-g++
Please note!
For 64-bit use: x86_64-w64-mingw32-g++
For 32-bit use: i686-w64-mingw32-g++

One option of compiling for Windows in Linux is via mingw. I found a very helpful tutorial here.
To install mingw32 on Debian based systems, run the following command:
sudo apt-get install mingw32
To compile your code, you can use something like:
i586-mingw32msvc-g++ -o myApp.exe myApp.cpp
You'll sometimes want to test the new Windows application directly in Linux. You can use wine for that, although you should always keep in mind that wine could have bugs. This means that you might not be sure that a bug is in wine, your program, or both, so only use wine for general testing.
To install wine, run:
sudo apt-get install wine

Install a cross compiler, like mingw64 from your package manager.
Then compile in the following way: instead of simply calling gcc call i686-w64-mingw32-gcc for 32-bit Windows or x86_64-w64-mingw32-gcc" for 64-bit Windows. I would also use the --static option, as the target system may not have all the libraries.
If you want to compile other language, like Fortran, replace -gcc with -gfortran in the previous commands.

I've used mingw on Linux to make Windows executables in C, I suspect C++ would work as well.
I have a project, ELLCC, that packages clang and other things as a cross compiler tool chain. I use it to compile clang (C++), binutils, and GDB for Windows. Follow the download link at ellcc.org for pre-compiled binaries for several Linux hosts.

From: https://fedoraproject.org/wiki/MinGW/Tutorial
As of Fedora 17 it is possible to easily build (cross-compile) binaries for the win32 and win64 targets. This is realized using the mingw-w64 toolchain: http://mingw-w64.sf.net/. Using this toolchain allows you to build binaries for the following programming languages: C, C++, Objective-C, Objective-C++ and Fortran.
"Tips and tricks for using the Windows cross-compiler": https://fedoraproject.org/wiki/MinGW/Tips

For Fedora:
# Fedora 18 or greater
sudo dnf group install "MinGW cross-compiler"
# Or (not recommended, because of its deprecation)
sudo yum groupinstall -y "MinGW cross-compiler"

Related

Install gcc in 32 bit ARM architecture

I've installed BuildRoot, to set a SoftetherVPN.
Well, my problem is that I necessary need to install GCC, in a 32-bit ARM Architecture, (obviously I've downloaded the 32 bit version).
I looked all over the internet, because of many problems:
My first idea was to install GCC using command sudo apt-get or sudo yum, but I can only find it in Debian Linux (Buildroot kernel, and its toolchain are very reduced).
At this point I tried to cross-compiling GCC but with no results, because it produced in shell output Library errors. And I also tried to paste those missing libraries yet achieve very little.
You could download some GCC cross-compiler. For example, if your PC is running some Debian-like Linux distribution -perhaps Ubuntu- you might install some gcc-7-arm-linux-gnueabi or similar (gcc-7-arm-linux-gnueabihf ...) package.
The SDK for your board is likely to provide some cross-GCC & cross-binutils.
You could build some cross binutils then some GCC cross-compiler from their source code (this is a bit more tricky).
Your cross-compiler would compile (for ARM) on your Linux-running PC, not on your ARM circuit board.

libwebsockets.h - no such file or directory, Ubuntu, arm-linux-gcc cross compiler

I am building a web server using the libwebsockets library on a TS-7800 board using the arm-linux-gcc cross compiler. Using Ubuntu. Installed CMake, OpenSSL, and libwebsockets and built the library per the instructions on Github.
I made a "hello world" C file which #includes libwebsockets.h
When I compile the executable with gcc, it compiles fine and the .exe runs.
When I compile with arm-linux-gcc, I get the following:
root#gordon-MS-7A39:/# arm-linux-gcc -o hellosockets /home/gordon/workspace/HelloCrossWorld/hello_cross.c
/home/gordon/workspace/HelloCrossWorld/hello_cross.c:3:27: libwebsockets.h: No such file or directory
It appears that arm-linux-gcc compiler cannot "see" the header file for libwebsockets. I'm guessing that the installation of the websockets library was successful because gcc can see it.
How do I enable the arm cross compiler to see the libwebsockets.h file?
Thank you for your input!
You'll need to add armhf architecture to your package management system. Perform the following actions as super user:
dpkg --add-architecture armhf
apt update
apt install libwebsockets-dev:armhf
Make sure you're also using the armhf toolchain:
apt install binutils-arm-linux-gnueabihf g++-arm-linux-gnueabihf
Alternatively, take a look at Buildroot
I was unaware of the -I and -L preprocessor options for gcc and arm-linux-gcc.
I was able to add libraries to the project and will look into creating makefiles for the project.

C: GTK+ 3.0 (3.20) - Cross-Compile from GNU/Linux (Arch Linux) to Windows

I need to cross-compile GTK+ application from GNU/Linux (Arch Linux) to Windows. I have already tried to use mingw32, but it does not see all libraries (including c's standard library!). So, I need to: find the standard libary; find all other (glib, gio, gtk, etc) libraries. But there's a problemm - I cannot find them. I also cannot compile from Windows. What must I do?
You can find the binaries for gtk 3 for windows on source forge or on http://win32builder.gnome.org/.
Once you have extracted them, you can follow this tutorial steps:
1) Install the compilation toolchain
Install the GCC compiler for Windows (namely MinGW, Arch Linux doc of package):
#pacman -S migw-w64
Download the latest all-in-one bundle ZIP archive directly from the official website (here's a direct link)
Adapt GTK+ to its location : In a terminal, move to the "gtk3-win32" folder you just created. For example: $cd /opt/gtk3-win32
then do :
find -name '*.pc' | while read pc; do sed -e "s#^prefix=.*#prefix=$PWD#" -i "$pc"; done
2) Compile
We will tell pkg-config to locate GTK+3 libraries in our custom path. If you extracted to /opt/gtk3-win32:
export PKG_CONFIG_PATH=/opt/gtk3-win32/lib/pkgconfig
We are ready to compile a sample C source ! Let's use a command in this style :
i586-mingw32msvc-gcc source.c -o executable.exe `pkg-config --cflags --libs gtk+-3.0
A new executable should have been created if everything went well.
It won't run on our Linux system, because it's targeting Windows !
3) Release
Create a folder containing the binary and the Windows .dll files. If you extracted to /opt/gtk3-win32:
mkdir ~/distri
cp executable.exe ~/distri
cp /opt/gtk3-win32/bin/*.dll ~/distri
Here we go ! Transfer this folder to a Windows box. Double-click on the executable and...
The following instructions are for gcc 5.3.0 (thread model: posix) and gtk+ 3.20.4
Install msys2 on windows
Install gtk3 files and copy the files
After installation, in msys2 shell
pacman -S mingw-w64-i686-gtk3
cd /mingw32
tar cfz c:/temp/mingw32.tar.gz
Ensure matching gcc version on Arch Linux
For gcc 5.3.0, edit /etc/pacman.conf
[core]
Server = https://archive.archlinux.org/repos/2016/05/10/$repo/os/$arch
[extra]
Server = https://archive.archlinux.org/repos/2016/05/10/$repo/os/$arch
[community]
Server = https://archive.archlinux.org/repos/2016/05/10/$repo/os/$arch
In case of upgrade
pacman -Syu
In case of downgrade
pacman -Syyuu
Later Arch Linux version seem to have gcc 6.1.1 that might be incompatible with msys2 gcc. A virtual machine might be a good idea for an Arch Linux installation that will not be upgraded (i.e. no security updates).
Copy files to linux
Unpack mingw32.tar.gz on linux, for example /opt/mingw32
Modify pkg-config files
perl -pi.bak -e 's,^prefix=.*,prefix=/opt/mingw32,' /opt/mingw32/lib/pkgconfig/*.pc
Set PKG_CONFIG_PATH
For example, before executing a configure script
export PKG_CONFIG_PATH=/opt/mingw32/lib/pkgconfig

How to create .exe executable file in Linux Ubuntu by .c source file [duplicate]

I have written some effects in C++ (g++) using freeglut on Linux, and I compile them with
g++ -Wall -lglut part8.cpp -o part8
So I was wondering if it is possible to have g++ make static compiled Windows executables that contains everything needed?
I don't have Windows, so it would be really cool, if I could do that on Linux :)
mingw32 exists as a package for Linux. You can cross-compile and -link Windows applications with it. There's a tutorial here at the Code::Blocks forum. Mind that the command changes to x86_64-w64-mingw32-gcc-win32, for example.
Ubuntu, for example, has MinGW in its repositories:
$ apt-cache search mingw
[...]
g++-mingw-w64 - GNU C++ compiler for MinGW-w64
gcc-mingw-w64 - GNU C compiler for MinGW-w64
mingw-w64 - Development environment targeting 32- and 64-bit Windows
[...]
Suggested method gave me error on Ubuntu 16.04: E: Unable to locate package mingw32
===========================================================================
To install this package on Ubuntu please use following:
sudo apt-get install mingw-w64
After install you can use it:
x86_64-w64-mingw32-g++
Please note!
For 64-bit use: x86_64-w64-mingw32-g++
For 32-bit use: i686-w64-mingw32-g++
One option of compiling for Windows in Linux is via mingw. I found a very helpful tutorial here.
To install mingw32 on Debian based systems, run the following command:
sudo apt-get install mingw32
To compile your code, you can use something like:
i586-mingw32msvc-g++ -o myApp.exe myApp.cpp
You'll sometimes want to test the new Windows application directly in Linux. You can use wine for that, although you should always keep in mind that wine could have bugs. This means that you might not be sure that a bug is in wine, your program, or both, so only use wine for general testing.
To install wine, run:
sudo apt-get install wine
Install a cross compiler, like mingw64 from your package manager.
Then compile in the following way: instead of simply calling gcc call i686-w64-mingw32-gcc for 32-bit Windows or x86_64-w64-mingw32-gcc" for 64-bit Windows. I would also use the --static option, as the target system may not have all the libraries.
If you want to compile other language, like Fortran, replace -gcc with -gfortran in the previous commands.
I've used mingw on Linux to make Windows executables in C, I suspect C++ would work as well.
I have a project, ELLCC, that packages clang and other things as a cross compiler tool chain. I use it to compile clang (C++), binutils, and GDB for Windows. Follow the download link at ellcc.org for pre-compiled binaries for several Linux hosts.
From: https://fedoraproject.org/wiki/MinGW/Tutorial
As of Fedora 17 it is possible to easily build (cross-compile) binaries for the win32 and win64 targets. This is realized using the mingw-w64 toolchain: http://mingw-w64.sf.net/. Using this toolchain allows you to build binaries for the following programming languages: C, C++, Objective-C, Objective-C++ and Fortran.
"Tips and tricks for using the Windows cross-compiler": https://fedoraproject.org/wiki/MinGW/Tips
For Fedora:
# Fedora 18 or greater
sudo dnf group install "MinGW cross-compiler"
# Or (not recommended, because of its deprecation)
sudo yum groupinstall -y "MinGW cross-compiler"

cross compile git repository with mingw under Ubuntu

I have a git repository which I can build with:
./autogen.sh
./configure
make
sudo make install
Now I want to cross compile it for windows with the mingw32/mingw-w64 cross compiler.
I tried it with
export CXX=/usr/bin/x86_64-w64-mingw32-gcc
but that didn't work for me.
I have configure eclipse that I can compile windows executables as well, that works, but only with plane c code. Now I need external libraries, which I have to compile for windows as well.
As described on a(n old, but mostly correct) page of the MinGW-w64 wiki (written by yours truly a long time ago):
./configure --host=x86_64-w64-mingw32
should do the trick if the package doesn't need special handling for Windows.

Resources