how to build a linux-based program for window? - linux

I want to build a inux-based program fully standalone for windows.
And I try to use mingw64 on ubuntu with the command
./configure --host=x86_64-w64-mingw32 --disable-shared --enable-static LDFLAGS="-L /usr/local/lib/"
make
But it failed with
src/libcork/posix/subprocess.c:14:24: fatal error: sys/select.h: No such file or directory
#include <sys/select.h>
^
compilation terminated.
Here is the source code in github:
https://github.com/shadowsocks/simple-obfs
So how can I build the program fully standalone for windows?
May I try the cygwin or msys2?
update:
I find another source code which has been ported.
But when I compiled it with
./configure --host=x86_64-w64-mingw32 CFLAGS="-static" LDFLAGS="-static"
I get an executable file which still needs the libssp-0.dll.
So how can I make the libssp be static linked?
Here is the new source code: https://github.com/Windendless/simple-obfs

I don't think that the functionality in select.h is available on the MinGW version of gcc, because it is not available in the Windows platform. If you're building something that was intended to be built on Windows, then you might find that there are options you can pass to configure that enable a different, Windows-friendly way of achieving the same thing. If you're building something that was only really intended to be used on Linux, then you might be out of luck. In that case, your choices really amount to:
Modify the code so it doesn't require select.h. Some of that functionality might already exist in the Windows Sockets API, with a somewhat different implementation. However, it might equally well not, in which case you'll have to do some work.
Use a Windows compatibility layer that is more feature-rich than MinGW. Cygwin might well work, as might the Windows Subsystem for Linux in Windows 10. However, none of these approaches amounts to building a "standalone" application, because they require some supporting infrastructure.

Related

Cross-compilation targeting Cygwin with Linux host

Is it possible to cross-compile a program targeting a Cygwin environment from a Linux/Unix host? I'm mostly trying to avoid using a Windows WM for building a collection of programs.
From the website:
Cygwin is not:
a way to run native Linux apps on Windows. You must rebuild your application from source if you want it to run on Windows.
mingw-w64 is not enough, I need a full POSIX platform. I'm also wondering if it's possible to target MSYS2.
Perhaps I'm not understanding correctly, and one doesn't need to compile in Cygwin but only mingw-w64 is necessary.
Thanks!
It is possible to compile for Cygwin from Linux.
The cygwin1.dll itself is built in such way.
Packages/libraries are available at:
https://copr.fedorainfracloud.org/coprs/yselkowitz/cygwin/
Of course only a minimal set of tools/libraries is available

Cross-platform build under Windows targeting Linux using CMake

I am developing a software in C++ on windows 32-bit (using MSVC++), but since I want to be able to use my software on every platform, I have decided to use CMake as my build generator.
Therefore, I am still just a beginner in CMake. From the CMake tutorials, I understand that in order to cross compile codes, first a toolchain simulating the target platform should be installed on the host platform. Then using the appropriate target-platform C and C++ compilers provided by this toolchain, CMake would be able to generate makefiles etc.
Now, I want to build my code for Linux platform(GNU/Linux) on a Win32 platform. I tried doing the above procedure using CMake combined with Cygwin and using gcc and g++ as compilers. It built fine, created makefiles, and when I issued "make" in Cygwin terminal, the generated makefiles were "made". Now I have got an executable which I was hoping would run on Linux platform. But on Linux I get the error: bash cannot execute binary file.
Using command file executablename, I realized the executable which is made by the above procedure is of type PE32 which is only for Windows.
Now my question is: Is my understanding of cross-platform build procedure using cmake correct?Or should I just use another Linux toolchain under windows to get a Linux ELF executable? What toolchains come to your mind which would give me what I want?
Many thanks
Setareh
You will want to look here: cmake-toolchains(7) if you do cross compiling. However, I would suggest that you install a Linux VM like virtual box on your windows machine and build naively on Linux. It will compile much faster and you will not have to worry about cross compiling. You can mount the windows disk from the linux VM so you can share the same source tree. The linux VM will compile much faster than gcc running under windows.
Your understanding of CMake is correct... it will determine how to create the build system you request (or is default for the platform you are currently on) based on rules in your CMakeLists.txt file. However, this won't necessarily help you compile for linux on a windows machine if you don't have something installed that can target linux.
To compile targeting linux, you will need to use a linux compiler. The link posted by #stjin tells you how to install one on cygwin. Then, to set up your CMake build, do this in the terminal:
CC=gcc-linux CXX=g++-linux cmake . [options]
This will tell CMake to locate the special linux targeted compilers. Hopefuly, after compiling with these compilers you will be able to run on linux.

linux, freepascal, fp-ide: No debugger support availble. How to enable debugger?

How to enable debugger in fp-ide? I read somewhere that I should compile fp-ide from sources, but I don't know how to do this. Can someone help me?
Get the generic linux tar installer (fpc-2.6.0.x86_64-linux.tar) for FPC from http://www.freepascal.org/down/x86_64/linux-hungary.var It comes with a precompiled IDE with integrated debugger support and it works fine at least on 12.04 LTS.
I wasn't able to find a PPA for fp-ide, but I can describe how the CLI IDE is compiled on Arch Linux as documented in the repository. Do note that compiling will not enable the debugger in the CLI, as it seems to be an incompatibility between gdb and fp (fp-ide) according to e.g. this bug report in Debian. On Arch Linux, the fpc package also doesn't support the debugger in fp by design (it is explicitly disabled using the NOGDB flag).
Anyhow, here goes the compilation process:
Make sure you have FreePascal installed already, as you need it to compile the IDE
Download the source tarball
Extract the tarball to a location of your convenience and cd into that directory
Execute the following code from within your shell:
pushd fpcsrc/compiler
fpcmake -Tall
popd
make build
make -j1 install
# in Arch, the switch "NOGDB=1" is present in both make lines
That should compile the IDE and install it (you can even try to integrate it in dpkg by using checkinstall instead of make install, but take a look at the Arch PKGBUILD to see an example of what might be needed).
But why do you use the command line IDE fp instead of lazarus? With lazarus you can also make console applications and it offers much more features (e.g. working debug support).

running binary on different machine causes segfault

I'm not well versed in how linking happens in c++
I have a binary that i compiled on one machine and i'd like to copy it and run it on another machine.
I would expect this to work, because the libraries are the same on both machines (i think!) and the version of linux is the same (same kernel, etc.) However, when i copy it over... it appears to segfault in one of the libraries i am dynamically linking when i run it.
It runs like butter on the machine that i compiled it on. But on the machine that i scp'd it over to, when i run the binary, it instantly segfaults on a std::string::compare in a call stack with some functions in one of the libraries i am dynamically linking.
i tried installing the libraries again on both machines and doing ldconfig, but same results.
any ideas on how to debug these kind of weird segfaults caused by dynamic linking issues?
Well, it might help narrow down the problem if you run the program in a debugger. When compiling, add the -g -ggdb arguments to the g++ command, then when running the program, use the command gdb ./executable (you may need to install gdb first.) At the gdb prompt, type run and your program will run until it segfaults. Then you can try to figure out what went wrong.
There are plenty of tutorials for using gdb (the GNU debugger) online.
Sounds like a binary compatibility issue. This SO link might shed some light:
Creating a generic binary in linux for all x86 machines

Compiling apache modules for Windows and Linux distros

I have written an Apache2 module in C, now I am at the point of distributing the module.
I am not sure of the standard practices regarding the distribution of Apache modules, so I am considering to provide binaries for some of the more common platforms, like Windows and Ubuntu, etc.
I have two questions to ask-
Will the module compiled on my Linux distro (Ubuntu 10.04) will be able to run on other Linux distros as well ? (I only have one machine so can't test it). By the way the compiled module is a shared object file.
I couldn't find any reference or guide on how to compile apache modules on windows, so it would be very kind of you if you could give some heads up regarding this .
Thanks.
The answer is...possibly. If the apache module and the other system were compiled with a similar version of gcc then everything should be fine. If they are not the same then there is a chance for problems. It depends on the gcc version, and the options you passed in to gcc for compilation.
Someone else would be more qualified to go into specifics of how to make a binary more portable between distros.
CMake is a very popular tool for compiling C code that works in windows. http://www.cmake.org/cmake/resources/software.html. You should be able to install it with apt-get.

Resources