Compile EXPAT to statically-linked .a on Windows - c

I am writing C program on Windows with MingW and want to use EXPAT XML library. I want to compile my program statically, so I need static .a library.
Is there any way to compile EXPAT to .a static, independent library on Windows?

If you download official releases from Expat Sourceforge project page you won't be able to compile and install the library with MSYS nor MinGW. You will need an installation of CygWin to configure, make and install.
To compile and install native MinGW library you need modified version of configure script and libtool. Just download expat source tarball provided by MinGW instead of main original expat. Also you may want use precompiled binaries instead

I just downloaded and built the package, it appears to have made a static library by default. What's not working for you?

You can get dynamic and static version of expat using MinGW package installer:
open a cmd or shell
update the package list: mingw-get update (optional)
install the package(s) you need:
dynamic (.dll): mingw-get install expat
static (.a and .dll.a): mingw-get install libexpat
depending on what you've chosen, you'll find ...
dynamic lib in <your mingw>/bin, file: libexpat-1.dll
static lib in <your mingw>/lib, files: libexpat.a, libexpat.dll.a, libexpat.la
Package overview:
package expat: Dynamic libexpat-1.dll incl. xmlwf.exe
package libexpat: Static libexpat.a, libexpat.dll.a and libexpat.la

Related

How to use C library installed with vcpkg on linux?

I'm trying to install libwebsockets C library with vcpkg according to the instruction. And don't understand something.
OS - Ubuntu 20.04
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg install [library-name]
Library is installed and what to do next? How to compile test files with the library?
The question is more about how to use vcpkg on linux.
You can give an example of another library installed with vcpkg.
vcpkg is a C/C++ package manager, it is very necessary in windows.
However, in ubuntu, itself provides a very complete package management mechanism.
Therefore, even if you are building a cross platform software system, do not use vcpkg in ubuntu :)
You can try this:
$> sudo apt install libwebsockets-dev
In this way, the libwebsockets header files and library files you need have been installed and can be used directly.
Here is another example:
I want to install gtkmm4 in ubuntu 20.04 LTS, since gtkmm4 is not available for apt download I'm installing it with vcpkg.
for simplification, I'm setting VCPKG_DIR to the vcpkg directory I cloned.
export VCPKG_DIR=/path/to/vckpg
Then for a C++ program, you can write CMake file like below
PROJECT(gtkmmtest)
cmake_minimum_required(VERSION 3.10)
set(VCPKG_DIR $ENV{VCPKG_DIR})
include(${VCPKG_DIR}/scripts/buildsystems/vcpkg.cmake) # --> important
# Use the package PkgConfig to detect GTK+ headers/library files
FIND_PACKAGE(PkgConfig REQUIRED)
FIND_PACKAGE(Threads REQUIRED)
pkg_check_modules(GTK4 REQUIRED gtk4)
PKG_CHECK_MODULES(GTKMM gtkmm-4.0)
include_directories(${GTK4_INCLUDE_DIRS})
include_directories(${GTKMM_INCLUDE_DIRS})
link_directories(${VCPKG_DIR}/packages/gtk_x64-linux/lib)
link_directories(${GTK4_LIBRARY_DIRS})
add_definitions(${GTK4_CFLAGS_OTHER})
target_link_libraries(${GTKMMTEST} PRIVATE ${GTK4_LIBRARIES} ${GTKMM_LIBRARIES} pthread)
Complete CMake file can be found here
You can still use the the standard include_directories and link_directories if there is no PkgConfig avilable.
eg:
include_directories(${VCPKG_DIR}/packages/gtkmm_x64-linux/include/gtkmm-4.0/)
Library is installed and what to do next? How to compile test files with the library?
The question is more about how to use vcpkg on linux.
The answer to this question really depends on your buildsystem and the port/library you want to use and not the platform itself.
In the case of libwebsockets libwebsockets-config.cmake get installed so you could use CMake and do a find_package(libwebsockets CONFIG REQUIRED) to get the imported targets the port exports within LibwebsocketsTargets.cmake. Of course this requires setting CMAKE_TOOLCHAIN_FILE to the vcpkg toolchain (<vcpkg_root>/scripts/buildsystems/vcpkg.cmake) or including it before the first project() in your CMakeLists.txt (more details are mentioned in the vcpkg docs which you hopefully read....)
Other libraries/ports might export *.pc files. For these FindPkgConfig.cmake can be used directly (see CMake docs) or you can setup PKG_CONFIG_PATH and prepend <vcpkg_root>/installed/<triplet (here probably: x64-linux)>/(debug/)lib/pkgconfig for other buildsystems like autotools or manual makefiles etc.
In the end how to use vcpkg or more precisly the libraries from it depends on what buildsystem you intend to use.

import libssh library in Visual Studio 2017

I'm currently trying to include the libssh library on Visual Studio 2017.
I already downloaded libssh but I don't know exactly what am I supposed to do with cmake. Where should I include files in Visual studio?
What you downloaded is the source code of libssh. So before you can link it to any of your own projects, you need to build libssh first. This were cmake comes in. CMake is the build system used for libssh.
In the source tree, which you have downloaded, you will find a file named INSTALL. It contains descriptions about all the prerequisites and a how you can use cmake to build libssh yourself.
If you prefer it, you can alternatively download a prebuilt version of libssh from https://www.libssh.org/files/win32/0.5/. The downside is, this is a quite dated version.
You can use vcpkg to download C++ libraries like libshh through command prompt. In this way the required dll's will be automatically include in your project directory, once you include the related header file in your project and compile it. See https://www.libssh.org/get-it/.

How to build protobuf-c in windows using mingw?

I can successfully build protobuf in Windows, it result in three .lib files, but don't know how to build protobuf-c in Windows, there are no documents about it.
i was able to do that with Cygwin:
you can download the sources from here https://github.com/protobuf-c/protobuf-c
depending on the protobuf that you've compiled you can download the "right" version of the protobuf-c.
generally speaking, to build with Cygwin you'll need to add on top of the default installation:
gcc and the tools around it.
tools to build protobuf and protobuf-c as documented in the Github of each (https://github.com/protobuf-c/protobuf-c). build protobuf again i suspect for Cygwin to recognize it?
build as told (same for both):
./autogen.sh
./configure --prefix=/usr
make
make install

Windows: How to build X264.lib instead of .dll

I downloaded the X264 source and installed mingw.
Step 1:
Executed this in the MINGW bash:
./configure --disable-cli --enable-shared --enable-win32thread -
-extra-ldflags=-Wl,--output-def=libx264.def
and then 'make'
Step 2:
Renamed the libx264-142.dll to libx264.dll and Opened up VS2012 Command Prompt and executed this:
LIB /DEF:libx264.def
which gave me libx264.lib and object libx264.exp
Step 3:
Included the lib file in a VS2012 project which uses the X264 API.
Problem:
When I start the project I get the following error message:
"The program can't start because libx264.dll is missing from your computer"
Question:
Why is it looking for the dll when I'm linking the static library in?
How do I resolve this? I would like to build a static X264 library which I can link in with my project.
EDIT:
I just had to put the dll in the same directory as the project executable.
However - My question still stands: How do I build a static x264 library? So I don't need the dll?
After the latest update of x264 you can build static library usable by MSVS project. For such library compilation you will need:
MSYS and MSVS 2013 Update 2 (express version [for Windows Desktop] would also work if you install Update 2)
run "VS2013 x86 Native Tools Command Prompt" or "VS2013 x64 Native Tools Command Prompt" depending what version (32 or 64-bit) you want to build
change dir to x264 path and run MSYS shell (sh)
from shell run "CC=cl ./configure --disable-cli --enable-static" for x264 configuring
run "make" which should build libx264.lib usable from MSVS
P.S. MSVS builds would be a little bit slower than one build by MinGW
Matthew Oliver has a GIT repository of a patched x264 source tree (https://github.com/ShiftMediaProject/x264) that compiles natively in VS2013 update 2 and later. It requires installing a YASM version for VS.
It worked pretty much straight out of the box for me, though I did have to change the VSYASM parameter "-f Win32" to "-f win32" for a 32bit build
Take a look here: http://siliconandlithium.blogspot.no/2014/03/building-x264-on-windows-with-visual.html
Static lib is not possible in windows as per my knowledge.

Cross compile GTK+ application from Linux to Windows?

How can I cross compile my GTK+ app (written in C) from Linux to Windows? Could I just replace the "gcc" command with "mingw32"?
Fedora has a great mingw32 cross-compiler toolchain which comes with lots of precompiled libraries, including GTK+ and gtkmm. For most applications you just need to install the cross-compiler and the cross-compiled GTK+ libraries:
yum install mingw32-gcc mingw32-gtk2
Once everything needed is installed, compiling your application is just the matter of running "mingw32-configure" followed with "make".
More information at the project page https://fedoraproject.org/wiki/MinGW
You can use mingw-cross-env - all you have to do then is to change your CC/CXX environment path to use the i686-mingw32- prefix and export the mingw-cross-env bin dirs (both) to your PATH variable (or if you are using autotool it's even easier) - see the documentation on the homepage.
There is actually a project called MXE that does exactly this.
Pre-build package
You can download my pre-build binaries if you want.
Build from source
You can also build the code from scratch ideally also applying this PR to update to the latest GTK 3.24 version.
MXE has a easy wrapper (x86_64-w64-mingw32.static-cmake) to cross-build your project towards Windows, while using Linux. Allowing to evenly statically build your project into a single .exe file! Of course shared builds (x86_64-w64-mingw32.shared-cmake) are also supported. The example wrapper scripts are meant for CMake based projects.
Before you can build your project with MXE, you need to build the GTK3 from source-code. (There are some Linux packages available, but mostly out-dated). If you are using C++, you can also build gtkmm3. Since you are in place C, you only need to build gtk3.
git clone https://github.com/mxe/mxe.git
Become root user: su
mv mxe /opt/mxe
cd /opt/mxe
Build the MXE project yourself:
For static builds under Windows 64-bit for GTK3 & Gtkmm3:
sudo make gtk3 gtkmm3 -j 16 MXE_TARGETS='x86_64-w64-mingw32.static' MXE_PLUGIN_DIRS='plugins/gcc12'
For shared build to Windows 64-bit (again GTK3 + Gtkmm3):
sudo make gtk3 gtkmm3 -j 16 MXE_TARGETS='x86_64-w64-mingw32.shared' MXE_PLUGIN_DIRS='plugins/gcc12'
More info see the tutorial steps on MXE.cc.
Once you done the cross-compile environment / MXE build. Now you can use the CMake wrapper scripts I mentioned earlier. Those scripts are located in the /opt/mxe/usr/bin/ directory.
The scripts (like x86_64-w64-mingw32.static-cmake) can now be used to compile your project towards Windows, while using the Linux operating system. The build result would be an Windows .exe.
Disclaimer: I personally created this PR for MXE to update GTK to the latest 3.24.x release.

Resources